M
M
Michael2017-05-03 23:36:38
Django
Michael, 2017-05-03 23:36:38

How to make two m2m fields with a link to one table?

Model example

class Naznach(models.Model):

    GROUP = (
        ('Продукты', 'Продукты'),
        ('Общепит', 'Общепит'),
        ('Другое', 'Другое')
    )

    group = models.CharField("Группа", max_length=30, choices=GROUP, default="F")
    options = models.CharField("Назначения", max_length=100)

    def __str__(self):
        return self.options


class Client(models.Model):

    naznach_one = models.ManyToManyField(Naznach, verbose_name="Назначение №1")
    naznach_two = models.ManyToManyField(Naznach, blank=True, verbose_name="Назначение №2")

The bottom line is to make two fields for selection from the same table.
But an error is thrown
8c5687c2853348159ac5ab0acb7b509f.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maks Nikolaev, 2017-05-04
@Maks113

related_nameyou need to add or remove a backlink in each field related_name='+'. It also is written in an error. I advise you to use Google more often.

class Client(models.Model):
    naznach_one = models.ManyToManyField(Naznach, verbose_name="Назначение №1", related_name='naznach_one_set' )
    naznach_two = models.ManyToManyField(Naznach, blank=True, verbose_name="Назначение №2" related_name='naznach_two_set')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question