1
votes
class Professional(models.Model):
    ...
    favoriting_customers = models.ManyToManyField(
        'customers.Customer', blank=True,
        through='favorites.FavoriteProfessional')

    recommending_customers = models.ManyToManyField(
        'customers.Customer', blank=True,
        through='recommendations.ProfessionalRecommendation')

I get no errors when I delete on of the ManyToMany fields. However, I get SystemCheckError when I run 'python manage.py makemigrations'.

ERRORS: professionals.Professional.favoriting_customers: (fields.E304) Reverse accessor for 'Professional.favoriting_customers' clashes with reverse accessor for 'Professional.recommending_customers'. HINT: Add or change a related_name argument to the definition for 'Professional.favoriting_customers' or 'Professional.recommending_customers'. professionals.Professional.recommending_customers: (fields.E304) Reverse accessor for 'Professional.recommending_customers' clashes with reverse accessor for 'Professional.favoriting_customers'. HINT: Add or change a related_name argument to the definition for 'Professional.recommending_customers' or 'Professional.favoriting_customers'.

1

1 Answers

1
votes

As suggested by the HINT, you need to use related_name to avoid clashes on backward relations. You are going to need this every time you have two fields in the same model with a relation to the same object (customers.Customer in your case).

You can try something like this:

class Professional(models.Model):
    ...
    favoriting_customers = models.ManyToManyField(
        'customers.Customer', blank=True,
        through='favorites.FavoriteProfessional',
        related_name='favorites'
    )

    recommending_customers = models.ManyToManyField(
        'customers.Customer', blank=True,
        through='recommendations.ProfessionalRecommendation',
        related_name='recommendations'
    )

If you are not interested in backward relation to Professional table, you can disable it by using '+' as the related_name:

class Professional(models.Model):
    ...
    favoriting_customers = models.ManyToManyField(
        'customers.Customer', blank=True,
        through='favorites.FavoriteProfessional',
        related_name='+'
    )

    recommending_customers = models.ManyToManyField(
        'customers.Customer', blank=True,
        through='recommendations.ProfessionalRecommendation',
        related_name='+'
    )

Also, you should be careful with related_name