填充ManyToManyField
我models.py
填充ManyToManyField
class Ingredient(models.Model):
name = models.CharField(max_length=16, unique=True)
price = models.SmallIntegerField()
def __str__(self):
return self.name
class Topping(models.Model):
name = models.CharField(max_length=32)
ingredient = models.ForeignKey(Ingredient, related_name='indole',
blank=True, null=True, default='base')
def __str__(self):
return self.nome
class Pizza(models.Model):
nome = models.CharField(max_length=32, unique=True)
toppings = models.ManyToManyField(Topping)
def __str__(self):
return self.nome
在它的工作管理!我可以添加馅料,比萨等,但我想用一个脚本来填充。
我script
:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'recipt.settings')
import django
django.setup()
from core.models import *
def populate():
cheap = add_ingredient('Cheap', 3)
base = add_ingredient('Base', 5)
good = add_ingredient('Good', 10)
cheese = add_topping('Cheese', None)
tomato = add_topping('Tomato', None)
olive = add_topping('Olive', None)
simple = add_pizza('Simple', cheese) #just one toppings for now
complex = add_pizza('Complex', tomato)
def add_ingredient(name, price):
i = Ingredient.objects.get_or_create(name=name, price=price)[0]
i.save()
return i
def add_topping(name, ingredient):
t = Topping.objects.get_or_create(name=name, ingredient=ingredient)[0]
t.save()
return t
def add_pizza(name, toppings):
p = Pizza.objects.get_or_create(name=name, toppings=toppings)[0]
p.save()
return p
if __name__ == '__main__':
print ("Starting Core population script...")
populate()
这对于成分和馅料,但不是比萨剧本的工作。
我error
(抱歉格式):
Starting Core population script...
Traceback (most recent call last):
File "c:\Python34\Scripts\recipt\myvenv\lib\site-packages\django\db\mode ls\query.py", line 465, in get_or_create
return self.get(**lookup), False
File "c:\Python34\Scripts\recipt\myvenv\lib\site-packages\django\db\mode ls\query.py", line 387, in getself.model._meta.object_name
core.models.DoesNotExist: Pizza matching query does not exist.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "populate_core.py", line 437, in modulepopulate()
File "populate_core.py", line 63, in populatesimple = add_pizza('Simple', cheese)
File "populate_core.py", line 307, in add_pizzap = Pizza.objects.get_or_create(name=name, toppings=toppings)[0]
File "c:\Python34\Scripts\recipt\myvenv\lib\site-packages\django\db\mode ls\manager.py", line 122, in manager_methodreturn getattr(self.get_queryset(), name)(*args, **kwargs)
File "c:\Python34\Scripts\recipt\myvenv\lib\site-packages\django\db\mode ls\query.py", line 467, in get_or_createreturn self._create_object_from_params(lookup, params)
File "c:\Python34\Scripts\recipt\myvenv\lib\site-packages\django\db\mode ls\query.py", line 499, in _create_object_from_paramsobj = self.create(**params)
File "c:\Python34\Scripts\recipt\myvenv\lib\site-packages\django\db\mode ls\query.py", line 399, in createobj = self.model(**kwargs)
File "c:\Python34\Scripts\recipt\myvenv\lib\site-packages\django\db\mode ls\base.py", line 443, in initraise TypeError("'%s' is an invalid keyword argument for this function" % li st(kwargs)[0]) TypeError: 'toppings' is an invalid keyword argument for this function
任何帮助吗?我读的地方,我应该离开浇头空白,并在以后添加,但...
当你创建一个具有ManyToMany
场的数据库记录,你不能这样做正常。你必须创建对象,然后添加的东西到ManyToMany
领域。像这样的东西。
class Author(models.Model):
name = models.CharField(max_length=100)
class Article(models.Model):
name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
zach = Author("Zach Braff")
zach.save()
# Say Zach writes an article...
# You can't do this because the authors field could have many things in it.
a1 = Article(name="Scrubs remake coming?", authors=zach)
# Instead, you have to do this...
a1 = Article(name="Scrubs remake coming?")
a1.authors.add(zach)
a1.save()
你可能想要做的是用有效的替代get_or_create()
,就像这样。
p = Pizza.objects.filter(name=name, toppings=toppings)
# This is faster than `if p`
if p.exists():
return p
else:
p = Pizza.objects.create(name=name)
p.toppings.add(toppings)
p.save()
return p
我认为应该工作。
的IT工作,谢谢。
这里我在script
新代码:
simple = add_pizza('Simple', [cheese, tomato])
complex = add_pizza('Complex', [cheese, tomato, olive])
def add_pizza(name, toppings):
p = Pizza.objects.filter(name=name)
if p.exists():
pass
else:
p = Pizza.objects.create(name=name)
for t in toppings:
p.toppings.add(t)
p.save()
return p