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 get

self.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 module

populate()
file "populate_core.py", line 63, in populate

simple = add_pizza('simple', cheese)
file "populate_core.py", line 307, in add_pizza

p = 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_method

return 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_create

return 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_params

obj = self.create(**params)
file "c:\python34\scripts\recipt\myvenv\lib\site-packages\django\db\mode ls\query.py", line 399, in create

obj = self.model(**kwargs)
file "c:\python34\scripts\recipt\myvenv\lib\site-packages\django\db\mode ls\base.py", line 443, in init

raise 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

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -