0

so I am building an app with django django rest and I have some issues with many to many fields in my models,

in the app there are two models that are connected by a many to many field option and version. the problem is that I want to filter my option and get only the option related to a number of version and only those who have (Default True). here is my models

class Option(models.Model):
    Code_Option = models.CharField(max_length=20, primary_key=True)
    Nom_Option = models.CharField(max_length=100)
    option_Version = models.ManyToManyField(Version,through='Option_Version')

class Option_Version(models.Model):
    option = models.ForeignKey(Option, on_delete=models.CASCADE)
    version = models.ForeignKey(Version, on_delete=models.CASCADE)
    Default = models.BooleanField(default = False)

class Version(models.Model):
    Code_Version = models.CharField(max_length=20, primary_key=True)
    Nom_Version = models.CharField(max_length=200)
    Id_Modele = models.ForeignKey(Modele, on_delete=models.CASCADE, related_name='Version_set')

and here is what I tried to do

class Option_defaut_Version(ListAPIView):
    serializer_class = Option_Sereializer

    def get_queryset(self):
        id_version = self.kwargs['Id_Version'] 
//id version refers to Code_Version from Version (it's the PK) and I get it from the url ex: option/default/<srt:Id_Version>
        return Option.objects.filter(option_Version__version = id_version).filter(option_Version__default = "True")

and here is my sereilasers :

class Option_Sereializer(serializers.ModelSerializer):

    class Meta:
        model = Option
        fields = [
            'Code_Option',
            'Nom_Option',
            'option_Version'
        ]

class Option_Version_Sereializer(serializers.ModelSerializer):

    class Meta:
        model = Option_Version
        fields = [
            'option',
            'version',
            'Default'
        ]
class Version_Sereializer(serializers.ModelSerializer):

    class Meta:
        model = Version
        fields = [
            'Nom_Version',
            'Code_Version'
        ]

also I tried this:

class option_version_default(ListAPIView):
    serializer_class = Option_Version_Sereializer
    def get_queryset(self):
        return Option_Version.objects.all()

the result was an error:

AttributeError at /option/default/v1

type object 'Option_Version' has no attribute 'objects'

if anyone could help me I would apreciate it

ps: I saw a lot of other questions and answers also the doc from django django rest but it didn't help me

raouf
  • 23
  • 6
  • Welcome to SO, can you explain your problem more precisely? What is the error? I see that you use Case sensitive to describe your fields. So maybe there is the error: .filter(option_Version__Default=True) – Tobit Mar 17 '19 at 21:33
  • well the error is : FieldError at /option/default/v1 Related Field got invalid lookup: version (option/default/v1 is the url for the class and the "v1" is the version pk) – raouf Mar 17 '19 at 22:19
  • my problem is that it kind of don't reconginse the other field (default) in the manytomanyfield. I just want to filter my request by the two creteria version=v1 and default=true, but I didn't find something useful when it comes to custom manytomanyfield in other docs – raouf Mar 17 '19 at 22:25
  • also the problem is not "True" ===> True – raouf Mar 17 '19 at 22:28
  • Please edit your question and add the Option_Version_Serializer and what is Id_Version? na id or an Object? Did you check this Answer? https://stackoverflow.com/questions/2218327/django-manytomany-filter – Tobit Mar 18 '19 at 08:06
  • I edited my post with th sereializers, idèversion refers to Code_Version from Version. and yes I already checked that answer it was useful for another manytomany field but not this one. this one has a through and for some reason it doesn't act like a normal one – raouf Mar 18 '19 at 10:44
  • >type object 'Option_Version' has no attribute 'objects' Looks like an import issue. Did you import Option_Version? – Kyryl Havrylenko Mar 18 '19 at 11:40
  • or did you two methods/Models that are called Option_Version? – Tobit Mar 18 '19 at 11:44
  • -yes, I imported everything needed. - no, it was the only method called Option_Version. thank you all for your help, I made it work by migrating everything to Version instead of Option (manytomany field in version Model , views and sereializers in the version.sereializer file). I still don't know where is the problem in this, but at least it works. – raouf Mar 18 '19 at 12:23

0 Answers0