Django. Create object ManyToManyField error

2024/11/20 19:40:27

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 setUpaddresses=2)ValueError: "<GiftEn: GiftEn, GiftEn description, GiftEn short description>" 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?

Answer

By error description you need create (save) GiftEn object before add many2many:

giften = GiftEn.objects.create(gift_id=1,name="GiftEn",description="GiftEn description",short_description="GiftEn short description")giften.categories.add('1')
giften.partner.add('1')
giften.addresses.add(2)
https://en.xdnf.cn/q/118380.html

Related Q&A

No module named discord

Im creating a discord bot, but when I try to import discord, I am getting this error: Traceback (most recent call last):File "C:\Users\Someone\Desktop\Discord bot\bot.py", line 2, in <modu…

Calendar with tkinter (print the selected date)

I got this code online in order to create a calendar with tkinter:""" Simple calendar using ttk Treeview together with calendar and datetime classes. """ import calendar i…

Python Tkinter scrollbar in multiple tabs

I learned how to make a scrollable frame by embedding the frame in a canvas and then adding a scrollbar to it like this:def __add_widget_features(self, feat_tab):table_frame = ttk.Frame(feat_tab)table_…

ValueError: setting an array element with a sequence error is showing

I am trying to convert this column in float type from object type but it is giving this error. import pandas as pddf = pd.DataFrame({col1: [[-0.8783137, 0.05478287, -0.08827557, 0.69203985, 0.06209986]…

Are there any datetime.tzinfo implementations in C?

Ive been working on a Python library that uses a C extension module to do ISO 8601 parsing.Part of that work requires the creation of tzinfo objects, which is by far the slowest part of the parse. Call…

How to open telnet as a textfile rather than a binary file

So I was trying to use the read_until method in telnet but then ran into the error: Traceback (most recent call last): File "c:\Users\Desktop\7DTD Bot\test.py", line 44, in <module> tn.…

The algorithm for dividing the range of subnet

There is a interesting algorithm, wrt dividing the range of subnet.I have a subnet,such as 192.168.1.0/24 or 192.168.1.248/22, and so on. And we know that /24 or /22 stands for networks and (32 - 24) o…

Look if a string starts with the ending characters of another string?

I want to see if ending of one string is similar to starting of another stringif i have a string a="12345678" and b="56789" i want to update a as 123456789these two strings are in …

Dict and List Manipulation Python

I have two files one has key and other has both key and value. I have to match the key of file one and pull the corresponding value from file two. When all the key and value are in plain column format …

Python - input of file path

this code works fine when I put the path of the file myself. but when I want to get it from users raw_input() it doesnt work. what can I do?import string import randomprint "enter number between …