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 Valenta <tonightslasts...@gmail.com> wrote: > 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 <juj...@gmail.com> 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.