Home > Software design >  Django3.2 connection OneToOneField on related_name object has no attribute 'all'
Django3.2 connection OneToOneField on related_name object has no attribute 'all'

Time:11-07

I have a problem getting data through related_name, it can't find the attribute all(). =(enter image description here enter image description here

The picture shows my attempts, but they did not lead to a result.

CodePudding user response:

It's so clear...You can't use all() cause it's only a field. if you want to access all() why you just do not do:

AdvertisimentType.objects.all()

?

CodePudding user response:

I think @amir-mohammad-sedaghat-nia is right. You can do:

AdvertisimentType.objects.all()

but, if you mean to retrieve all objects without specifying the model class you can use a bit of "internals":

type.types._meta.model.objects.all()

or (worse imho):

type.types.__class__.objects.all()

Finally, I suppose that you would like to bind more than one AdvertisimentType to Advertisiment given that your related_name is "types" and that you aim to call the .all() method.

If this is the case: your OneToOneField should be replaced by a ManyToManyField (or a ForeignKey?). That way you can call the .all() method:

type.types.all()
  • Related