python - Populate a ManyToManyField -
my models.py
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
in admin work! can add topping, pizza, etc. want use script populate.
my script
:
import os os.environ.setdefault('django_settings_module', 'recipt.settings') import django django.setup() core.models import * def populate(): cheap = add_ingredient('cheap', 3) base = add_ingredient('base', 5) = 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 1 toppings complex = add_pizza('complex', tomato) def add_ingredient(name, price): = ingredient.objects.get_or_create(name=name, price=price)[0] i.save() return 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()
this script work ingredient , topping not pizza.
my error
(sorry formatting):
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 not exist.
during handling of above exception, 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' invalid keyword argument function" % li st(kwargs)[0]) typeerror: 'toppings' invalid keyword argument function
any please? read somewhere should leave toppings blank , add later but...
when create database record has manytomany
field, can't normally. have create object, , add things manytomany
field. this.
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() # zach writes article... # can't because authors field have many things in it. a1 = article(name="scrubs remake coming?", authors=zach) # instead, have this... a1 = article(name="scrubs remake coming?") a1.authors.add(zach) a1.save()
what might want replace get_or_create()
equivalent, this.
p = pizza.objects.filter(name=name, toppings=toppings) # faster `if p` if p.exists(): return p else: p = pizza.objects.create(name=name) p.toppings.add(toppings) p.save() return p
i think ought work.
Comments
Post a Comment