Re: Issue when saving a model form
thank you. Excluding city from the ModelForm seems to work as well... class AddRestaurantForm(ModelForm): rating = forms.IntegerField(widget=forms.Select(choices = RATING_CHOICE), required=False) city = forms.CharField(max_length=100) class Meta: model = Restaurant exclude = ('slug', 'city') On Nov 27, 9:10 pm, Steve Howellwrote: > I am not fully tracking to the problem here, but when it comes to > overriding save() behavior on forms, I find the following helper to be > handy: > > def build_instance_from_form(model_form): > instance = model_form.save(commit=False) > return instance > > Then in your view code do something like this: > > restaurant = build_instance_from_form(form) > # fix up data like city > restaurant.save() > > This can help you avoid all the complexity of overriding save() on the > form object and delegating to super(), etc., at least in some cases. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Issue when saving a model form
I am not fully tracking to the problem here, but when it comes to overriding save() behavior on forms, I find the following helper to be handy: def build_instance_from_form(model_form): instance = model_form.save(commit=False) return instance Then in your view code do something like this: restaurant = build_instance_from_form(form) # fix up data like city restaurant.save() This can help you avoid all the complexity of overriding save() on the form object and delegating to super(), etc., at least in some cases. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Issue when saving a model form
The problem is that city name is not unique. Can I pass the city instance in the overrided AddRestaurantForm's save method? thank you On Nov 27, 5:12 pm, Tim Valentawrote: > Try overriding your AddRestaurantForm's "save" method: > > def save(self, commit=True): > self.instance.city = get_object_or_404(City, > name=self.instance.city) > > # Finish by passing control back to the normal Django flow: > super(AddRestaurantForm, self).save(commit) > > I think that'd work. Sometimes 'super' causes recursion errors in > Django, so if that gives you problems, try calling the method all by > itself: > > # Finish by passing control back tot he normal Django flow: > forms.ModelForm.save(self, commit) > > Tim > > On Nov 27, 8:57 am, jul wrote: > > > Previous post wasn't finished. Wrong key :( > > Here's the complete one: > > > Hi, > > > I've got the ModelForm and Model shown below. > > I'm overriding the 'city' field to get a CharField instead of a City > > foreign key. > > When saving the Restaurant instance, it returns ""Restaurant.city" > > must be a "City" instance". > > How can I save the Restaurant instance with the City instance got or > > created before? > > > Thanks > > > def addRestaurant(request): > > > if request.user.is_authenticated(): > > > if request.method == 'POST': > > > form = AddRestaurantForm(request.POST) > > > if form.is_valid(): > > > geonameID = request.POST.get('city_geonameId','') > > country = Country.objects.get(code=request.POST.get > > ('city_countryCode','')) > > city, created = City.objects.get_or_create > > (geonameID=geonameID, country = country, name = form.cleaned_data > > ['city']) > > > if created: #stuff > > > city.save() > > > new_restaurant = form.save > > (commit=False) > > new_restaurant.city = city > > > class AddRestaurantForm(ModelForm): > > > rating = forms.IntegerField(widget=forms.Select(choices = > > RATING_CHOICE), required=False) > > city = forms.CharField(max_length=100) > > > class Meta: > > model = Restaurant > > exclude = ('slug') > > > class Restaurant(models.Model): > > > name = models.CharField(max_length=100) > > country=models.ForeignKey(Country) > > city=models.ForeignKey(City) > > street=models.CharField(max_length=100) > > street_number=models.PositiveSmallIntegerField() > > phone_number=models.CharField(max_length=16, blank=True, > > null=True) > > price_range=models.PositiveSmallIntegerField(blank=True, > > null=True) > > category=models.ManyToManyField(Category, blank=True, null=True) > > tag=models.ManyToManyField(Tag, blank=True, null=True) > > slug = models.SlugField(unique=True) > > > def get_absolute_url(self): > > return "/restaurant/%s/" % self.slug > > > def __unicode__(self): > > return self.name > > > def save(self): > > self.slug = slugify(self.name) > > super(Restaurant, self).save() -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Issue when saving a model form
Try overriding your AddRestaurantForm's "save" method: def save(self, commit=True): self.instance.city = get_object_or_404(City, name=self.instance.city) # Finish by passing control back to the normal Django flow: super(AddRestaurantForm, self).save(commit) I think that'd work. Sometimes 'super' causes recursion errors in Django, so if that gives you problems, try calling the method all by itself: # Finish by passing control back tot he normal Django flow: forms.ModelForm.save(self, commit) Tim On Nov 27, 8:57 am, julwrote: > Previous post wasn't finished. Wrong key :( > Here's the complete one: > > Hi, > > I've got the ModelForm and Model shown below. > I'm overriding the 'city' field to get a CharField instead of a City > foreign key. > When saving the Restaurant instance, it returns ""Restaurant.city" > must be a "City" instance". > How can I save the Restaurant instance with the City instance got or > created before? > > Thanks > > def addRestaurant(request): > > if request.user.is_authenticated(): > > if request.method == 'POST': > > form = AddRestaurantForm(request.POST) > > if form.is_valid(): > > geonameID = request.POST.get('city_geonameId','') > country = Country.objects.get(code=request.POST.get > ('city_countryCode','')) > city, created = City.objects.get_or_create > (geonameID=geonameID, country = country, name = form.cleaned_data > ['city']) > > if created: #stuff > > city.save() > > new_restaurant = form.save > (commit=False) > new_restaurant.city = city > > class AddRestaurantForm(ModelForm): > > rating = forms.IntegerField(widget=forms.Select(choices = > RATING_CHOICE), required=False) > city = forms.CharField(max_length=100) > > class Meta: > model = Restaurant > exclude = ('slug') > > class Restaurant(models.Model): > > name = models.CharField(max_length=100) > country=models.ForeignKey(Country) > city=models.ForeignKey(City) > street=models.CharField(max_length=100) > street_number=models.PositiveSmallIntegerField() > phone_number=models.CharField(max_length=16, blank=True, > null=True) > price_range=models.PositiveSmallIntegerField(blank=True, > null=True) > category=models.ManyToManyField(Category, blank=True, null=True) > tag=models.ManyToManyField(Tag, blank=True, null=True) > slug = models.SlugField(unique=True) > > def get_absolute_url(self): > return "/restaurant/%s/" % self.slug > > def __unicode__(self): > return self.name > > def save(self): > self.slug = slugify(self.name) > super(Restaurant, self).save() -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Issue when saving a model form
Previous post wasn't finished. Wrong key :( Here's the complete one: Hi, I've got the ModelForm and Model shown below. I'm overriding the 'city' field to get a CharField instead of a City foreign key. When saving the Restaurant instance, it returns ""Restaurant.city" must be a "City" instance". How can I save the Restaurant instance with the City instance got or created before? Thanks def addRestaurant(request): if request.user.is_authenticated(): if request.method == 'POST': form = AddRestaurantForm(request.POST) if form.is_valid(): geonameID = request.POST.get('city_geonameId','') country = Country.objects.get(code=request.POST.get ('city_countryCode','')) city, created = City.objects.get_or_create (geonameID=geonameID, country = country, name = form.cleaned_data ['city']) if created: #stuff city.save() new_restaurant = form.save (commit=False) new_restaurant.city = city class AddRestaurantForm(ModelForm): rating = forms.IntegerField(widget=forms.Select(choices = RATING_CHOICE), required=False) city = forms.CharField(max_length=100) class Meta: model = Restaurant exclude = ('slug') class Restaurant(models.Model): name = models.CharField(max_length=100) country=models.ForeignKey(Country) city=models.ForeignKey(City) street=models.CharField(max_length=100) street_number=models.PositiveSmallIntegerField() phone_number=models.CharField(max_length=16, blank=True, null=True) price_range=models.PositiveSmallIntegerField(blank=True, null=True) category=models.ManyToManyField(Category, blank=True, null=True) tag=models.ManyToManyField(Tag, blank=True, null=True) slug = models.SlugField(unique=True) def get_absolute_url(self): return "/restaurant/%s/" % self.slug def __unicode__(self): return self.name def save(self): self.slug = slugify(self.name) super(Restaurant, self).save() -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Issue when saving a model form
Hi, I've got the ModelForm and Model shown below. I'm overriding the 'city' field to get a CharField instead of a City foreign key. def addRestaurant(request): if request.user.is_authenticated(): if request.method == 'POST': form = AddRestaurantForm(request.POST) if form.is_valid(): geonameID = request.POST.get('city_geonameId','') country = Country.objects.get(code=request.POST.get ('city_countryCode','')) city, created = City.objects.get_or_create (geonameID=geonameID, country = country, name = form.cleaned_data ['city']) if created: print "hello1" city.save() class AddRestaurantForm(ModelForm): rating = forms.IntegerField(widget=forms.Select(choices = RATING_CHOICE), required=False) city = forms.CharField(max_length=100) class Meta: model = Restaurant exclude = ('slug') class Restaurant(models.Model): name = models.CharField(max_length=100) country=models.ForeignKey(Country) city=models.ForeignKey(City) street=models.CharField(max_length=100) street_number=models.PositiveSmallIntegerField() phone_number=models.CharField(max_length=16, blank=True, null=True) price_range=models.PositiveSmallIntegerField(blank=True, null=True) category=models.ManyToManyField(Category, blank=True, null=True) tag=models.ManyToManyField(Tag, blank=True, null=True) slug = models.SlugField(unique=True) def get_absolute_url(self): return "/restaurant/%s/" % self.slug def __unicode__(self): return self.name def save(self): self.slug = slugify(self.name) super(Restaurant, self).save() -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.