[Fixed]-Django – reverse query name clash

17👍

To avoid this you can use the related_name:

class Supplier(models.Model):
    unique_id = models.IntegerField(unique=True)
    name = models.CharField(max_length=255, unique=True)
    rating = models.FloatField(null=True)
    last_updated = models.DateTimeField(auto_now=True)
    default_tariff = models.ForeignKey('Tariff', related_name='+')

class Tariff(models.Model):
    name = models.CharField(max_length=255)
    supplier = models.ForeignKey(Supplier)
    end_date = models.DateField(null=True, blank=True)
    payment_method = models.ManyToManyField(PaymentMethod) 
    region = models.ManyToManyField(Region)

Edit from the comments:

When you set related_name="+" Django does not create a backwards relation. In this case, Tariff will not have a backwards relation to the Supplier model.

👤Gocht

5👍

The error message is a pretty correct statement on its own (you have omitted the error code given in the output, I think it was ‘E303’), but we need to understand what it actually means.

I will not ask why don’t you use Many-to-many relationship in this case, let’s focus on how "backward" object relations work:

Example:

from django.db import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    def __str__(self):
        return self.name

class Entry(models.Model):
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
    headline = models.CharField(max_length=100)
    def __str__(self):
        return self.headline

Following relationships “backward”

One-to-many relationship does not exist in a "forward" manner because it is the "many side", that holds the key, but Django creates API accessors for the “other” side of the relationship – the link from the related model to the model that defines the relationship. For example, a Blog object b has access to a list of all related Entry objects via the entry_set attribute: b.entry_set.all().

If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the “Retrieving objects” section above.

Follow the documentation link for more info/examples.

It’s because of these API accessors ‘related_name’ (complement this information with related_name in Abstract Classes) was implemented, to avoid naming collisions between field names and

  1. accessor_name(which would be the name of the manager),
    and/or
  2. query_name (which is the model name))

Let’s take a look at the Django code for better understanding:

This is where your error comes from:

def _check_clashes(self):
    """Check accessor and reverse query name clashes."""
    from django.db.models.base import ModelBase

    errors = []
    opts = self.model._meta

    # `f.remote_field.model` may be a string instead of a model. Skip if model name is
    # not resolved.
    if not isinstance(self.remote_field.model, ModelBase):
        return []

    # Consider that we are checking field `Model.foreign` and the models
    # are:
    #
    #     class Target(models.Model):
    #         model = models.IntegerField()
    #         model_set = models.IntegerField()
    #
    #     class Model(models.Model):
    #         foreign = models.ForeignKey(Target)
    #         m2m = models.ManyToManyField(Target)

    # rel_opts.object_name == "Target"
    rel_opts = self.remote_field.model._meta
    # If the field doesn't install a backward relation on the target model
    # (so `is_hidden` returns True), then there are no clashes to check
    # and we can skip these fields.
    rel_is_hidden = self.remote_field.is_hidden()
    rel_name = self.remote_field.get_accessor_name()  # i. e. "model_set"
    rel_query_name = self.related_query_name()  # i. e. "model"
    # i.e. "app_label.Model.field".
    field_name = '%s.%s' % (opts.label, self.name)

    # Check clashes between accessor or reverse query name of `field`
    # and any other field name -- i.e. accessor for Model.foreign is
    # model_set and it clashes with Target.model_set.
    potential_clashes = rel_opts.fields + rel_opts.many_to_many
    for clash_field in potential_clashes:
        # i.e. "app_label.Target.model_set".
        clash_name = '%s.%s' % (rel_opts.label, clash_field.name)
        if not rel_is_hidden and clash_field.name == rel_name:
            errors.append(
                checks.Error(
                    "Reverse accessor for '%s' clashes with field name '%s'." % (field_name, clash_name),
                    hint=("Rename field '%s', or add/change a related_name "
                          "argument to the definition for field '%s'.") % (clash_name, field_name),
                    obj=self,
                    id='fields.E302',
                )
            )

        if clash_field.name == rel_query_name:
            errors.append(
                checks.Error(
                    "Reverse query name for '%s' clashes with field name '%s'." % (field_name, clash_name),
                    hint=("Rename field '%s', or add/change a related_name "
                          "argument to the definition for field '%s'.") % (clash_name, field_name),
                    obj=self,
                    id='fields.E303',
                )
            )

    # Check clashes between accessors/reverse query names of `field` and
    # any other field accessor -- i. e. Model.foreign accessor clashes with
    # Model.m2m accessor.
    potential_clashes = (r for r in rel_opts.related_objects if r.field is not self)
    for clash_field in potential_clashes:
        # i.e. "app_label.Model.m2m".
        clash_name = '%s.%s' % (
            clash_field.related_model._meta.label,
            clash_field.field.name,
        )
        if not rel_is_hidden and clash_field.get_accessor_name() == rel_name:
            errors.append(
                checks.Error(
                    "Reverse accessor for '%s' clashes with reverse accessor for '%s'." % (field_name, clash_name),
                    hint=("Add or change a related_name argument "
                          "to the definition for '%s' or '%s'.") % (field_name, clash_name),
                    obj=self,
                    id='fields.E304',
                )
            )

        if clash_field.get_accessor_name() == rel_query_name:
            errors.append(
                checks.Error(
                    "Reverse query name for '%s' clashes with reverse query name for '%s'."
                    % (field_name, clash_name),
                    hint=("Add or change a related_name argument "
                          "to the definition for '%s' or '%s'.") % (field_name, clash_name),
                    obj=self,
                    id='fields.E305',
                )
            )

    return errors

This is how the accessor_name is born

def get_accessor_name(self, model=None):
    # This method encapsulates the logic that decides what name to give an
    # accessor descriptor that retrieves related many-to-one or
    # many-to-many objects. It uses the lowercased object_name + "_set",
    # but this can be overridden with the "related_name" option. Due to
    # backwards compatibility ModelForms need to be able to provide an
    # alternate model. See BaseInlineFormSet.get_default_prefix().
    opts = model._meta if model else self.related_model._meta
    model = model or self.related_model
    if self.multiple:
        # If this is a symmetrical m2m relation on self, there is no reverse accessor.
        if self.symmetrical and model == self.model:
            return None
    if self.related_name:
        return self.related_name
    return opts.model_name + ('_set' if self.multiple else '')

and this is the query_name (also in related.py)

def related_query_name(self):
    """
    Define the name that can be used to identify this related object in a
    table-spanning query.
    """
    return self.remote_field.related_query_name or self.remote_field.related_name or self.opts.model_name

Now, that you know how the naming is done and what the reverse relationship is, you’ll understand why

Reverse query name for ‘Supplier.default_tariff’ clashes with field name ‘Tariff.supplier’.

And how to solve it:

HINT: Rename field ‘Tariff.supplier’, or add/change a related_name argument to the definition for field ‘Supplier.default_tariff’.

Hope that helps. I just was working on a related issue ))

👤mgoetz

1👍

Try to use the following solution.It works for django==3.

class Supplier(models.Model):
    unique_id = models.IntegerField(unique=True)
    name = models.CharField(max_length=255, unique=True)
    rating = models.FloatField(null=True)
    last_updated = models.DateTimeField(auto_now=True)
    default_tariff = models.ForeignKey('Tariff', related_name="%(class)s_tarriff")

Leave a comment