A
A
Astrohas2017-07-28 18:08:35
Django
Astrohas, 2017-07-28 18:08:35

Why is ManyToMany not working?

class Product(models.Model):
         slug = models.SlugField()
class ModalProduct(models.Model):
    modal = models.ForeignKey('Modal'),
    product = models.ForeignKey(Product)
    weight = models.SmallIntegerField(default=0)

class Modal(models.Model):
    products = models.ManyToManyField(Product, through=ModalProduct, through_fields=('modal', 'product'))

With makemigrations I get
ERRORS:
offers.Modal.products: (fields.E338) The intermediary model 'offers.ModalProduct' has no field 'modal'.
offers.ModalProduct: (fields.E336) The model is used as an intermediate model by 'offers.Modal.products', but it does not have a foreign key to 'Modal' or 'Product'.

What could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Astrohas, 2017-07-28
@Astrohas

a very, very strange sight: the river outside the window is on fire, someone's house is wagging its tail, a dog is shooting from a gun, a boy almost ate a mouse, a cat with glasses is reading a book, an old grandfather flew in through the window, a sparrow grabbed grain, and how he screams flying away : that's what the comma means!

The comma after ForeignKey('Modal') is a masterpiece

N
ns5d, 2017-07-28
@ns5d

class Person(models.Model):
    name = models.CharField(max_length=50)

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(
        Person,
        through='Membership',
        through_fields=('group', 'person'),
    )

class Membership(models.Model):
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    invite_reason = models.CharField(max_length=64)

https://docs.djangoproject.com/en/1.11/ref/models/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question