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.