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