Search code examples
pythondjangodatabaseormattributeerror

Django3.2 connection OneToOneField on related_name object has no attribute 'all'


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.


Solution

  • 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()