Because of the dependants you might need to either manually add the tables to the db or if you don't have any values in that table delete all the rows in pizza and run syncdb again. Sometimes when working within the same class the syncdb doesn't change the database all the way (and rightly so). Give that a try and if it still doesn't work we'll try a different solution.
On May 31, 1:03 pm, Gio <[EMAIL PROTECTED]> wrote: > Thanks to you all, I took something from all the replies and I wrote > this modifying Michael code: > > class Topping(models.Model): > name = models.CharField(maxlength=255, unique=True) > > def __str__(self): > return self.name > > class Admin: > pass > > class Pizza(models.Model): > name = models.CharField(maxlength=255, unique=True) > > def __str__(self): > return self.name > > class Admin: > pass > > class PizzaTopping(models.Model): > pizza = models.ForeignKey(Pizza, > help_text = 'Toppings to go on Pizza: > num in admin is how many will show up in Pizza', > edit_inline = models.TABULAR, > num_in_admin = 3, > core=True) > topping = models.ForeignKey(Topping) > amount = models.IntegerField() > > I got problems in admin: I inserted a few toppings, now when I insert > a new pizza with his toppings the toppings and quantities aren't > saved. > No error messages, but inspecting database reveals no entries for the > auxiliary table pizza_pizzatopping > > Any hint? > I'd like to solve this problem, leaving a solution for the archives. > > Thanks, > Giovanni. > > On 31 Mag, 18:27, Michael Newman <[EMAIL PROTECTED]> wrote: > > > Nis; > > Thanks for catching that I left the manytomany field there. There is > > no need for that. > > revised code: > > > class Topping(models.Model): > > # ... > > > class Pizza(models.Model): > > # ... > > > class PizzaToppings(model.Model): > > pizza = models.ForeignKey(Pizza, help_text='Toppings to go on > > Pizza: num in admin is how many will show up in Pizza', > > edit_inline=models.TABULAR, num_in_admin=3) > > topping = models.ManyToOne(Topping) > > amount = models.IntegerField() > > #... > > > This will appear in the Pizza part of the admin as a stacked model and > > allow you to use or add as many as you would like. So you can remove > > the amount if you just want people to be able to add double cheese by > > adding a second cheese topping. > > > Thanks Nis and good luck Giovanni. > > > Mn > > > On May 31, 12:21 pm, Nis Jorgensen <[EMAIL PROTECTED]> wrote: > > > > Gio wrote: > > > > Hi, > > > > I searched about this subject and found only a few very old posts, so > > > > maybe there is a better solution now. > > > > > As you may guess the model I'd like to code is similar to the Pizza - > > > > Toppings you know from the "Creating Models" documentation: > > > > > class Topping(models.Model): > > > > # ... > > > > > class Pizza(models.Model): > > > > # ... > > > > toppings = models.ManyToManyField(Topping) > > > > > And what if I need to say that I can have two or three times the same > > > > topping on my pizza? Something like twice mozzarella cheese and 3 > > > > times green olives topping? > > > > > I though about an intermediary class and indeed this is the same > > > > solution found in those old posts mentioned before: > > > > > class Topping(models.Model): > > > > # ... > > > > > class ToppingAndQuantity(models.Model): > > > > amount = models.IntegerField() > > > > topping = models.ForeignKey(Topping) > > > > > class Pizza(models.Model): > > > > # ... > > > > toppings = models.ManyToManyField(ToppingAndQuantity) > > > > > I think it's ugly. > > > > Can you suggest me a better solution? > > > > It's that intermediary class really needed? > > > > I believe you should have > > > > class Topping(models.Model): > > > # ... > > > > class Pizza(models.Model): > > > # ... > > > > class PizzaTopping(models.Model): > > > amount = models.IntegerField() > > > topping = models.ForeignKey(Topping) > > > pizza = models.ForeignKey(Pizza) > > > > The "PizzaTopping" class is really a more elaborate specification of the > > > many-to-many relationship, so you don't need the ManyToManyField > > > anymore. Your model has the problem that two pizzas can share > > > (Mozarella, 2) - leading to problems if you modify one of them. > > > > You will want to do the invocations for making (topping, pizza) unique > > > as well, and possibly do some magic so that trying to add another > > > Mozarella will increment the quantity instead. > > > > Disclaimers: I am a novice Django user. The code is untested. > > > > Update: Between posting and reposting from the correct mail address, I > > > saw the response from Michael. He is using "ManyToOneField" which I > > > don't see in my documentation, and keeps the ManyToManyField(Topping) > > > which seems strange to me. I believe you will be able to put together a > > > good solution from the two posts. > > > > Nis --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---