Re: Issue when saving a model form

2009-11-27 Thread jul
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 Howell  wrote:
> 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

2009-11-27 Thread Steve Howell
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

2009-11-27 Thread jul
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  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  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

2009-11-27 Thread Tim Valenta
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.




Issue when saving a model form

2009-11-27 Thread jul
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

2009-11-27 Thread jul
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.