Can’t access “on_delete” argument of model field using _meta class (Django)

Title. I’m trying to access the on_delete argument of the field “org” here:

class OrgLocation(models.Model):
    org = models.OneToOneField(
        Org, primary_key=True, on_delete=models.CASCADE, blank=True
    )

And I’m trying to access it with:

OrgLocation._meta.get_field("org").on_delete

But when I run this, I get an attribute error:

AttributeError: 'OneToOneField' object has no attribute 'on_delete'

Trying to access primary_key works just fine though (and same for blank). Not sure what’s going on. Any suggestions?

  • Why do you want to access on_delete value? It’s not meant to be accessible. It’s for letting django know, that how django should behave when you delete objects that have relationships.

    – 

  • I’d like to access it for unit testing purposes

    – 

  • Did you try this stackoverflow.com/a/29423946/13861187

    – 

  • Did any of the answers below work?

    – 

  • Using remote_field to access the on_delete attribute did.

    – 

You may need to use the remote_field attribute on the field to access the on_delete attribute, e.g.

OrgLocation._meta.get_field("org").remote_field.on_delete

Try below.

include_hidden

doc

also look here

Leave a Comment