samedi 2 septembre 2017

Django. Create object ManyToManyField error.

I am trying to write tests for my models.

I try to create object like this:

    GiftEn.objects.create(gift_id=1,
    name="GiftEn",
    description="GiftEn description",
    short_description="GiftEn short description",
    categories='1',
    partner='1',
    addresses=2)

But i get this error: File "/home/stasman/giver/giver/giver/gift/test_gift.py", line 29, in setUp addresses=2)

ValueError: "" needs to have a value for field "giften" before this many-to-many relationship can be used.

Here is my model:

class GiftEn(models.Model):
    gift = models.ForeignKey(Private, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    description = models.TextField(max_length=1000)
    short_description = models.TextField(max_length=500)
    categories = models.ManyToManyField(CategoryEn,)
    partner= models.ManyToManyField(Partner,)
    addresses= models.ManyToManyField(AddressWhereTakeGift,)

    def __str__(self):
        return '%s, %s, %s' % (self.name, self.description, self.short_description)

    class Meta:
        ordering = ('-gift__id',)

How to create object for this model?

Aucun commentaire:

Enregistrer un commentaire