Re: Models, forms and inlineformset

2010-01-30 Thread andreas schmid
it works now :)
thank you!!

Stefan Nitsche wrote:
> On Sat, Jan 30, 2010 at 11:23, andreas schmid  > wrote:
>
> Stefan Nitsche wrote:
> >
> >
> > On Thu, Jan 28, 2010 at 10:15, andreas schmid
> 
> > >> wrote:
> >
> > and your form works on edit too?
> > i really cant understand why mine isnt...
> >
> > its giving me this MultiValueDictKeyError which i dont
> understand.
> >
> > Stefan Nitsche wrote:
> > > On Wed, Jan 27, 2010 at 14:04, andreas schmid
> > 
> >
> > > 
>  > >
> > > is this an add or an edit form? or both?
> > >
> > > i have a similar inlineformset and i need to loop over the
> > inline
> > > objects to set the foreignkey to the parent object like:
> > >
> > >if form.is_valid() and formset.is_valid(): # All
> validation
> > > rules pass
> > >new_idea = form.save(commit=False)
> > >new_idea.author = request.user
> > >new_idea = form.save()
> > >formset_models = formset.save(commit=False)
> > >for f in formset_models:
> > >f.idea = new_idea
> > >f.save()
> > >return
> > > HttpResponseRedirect(new_idea.get_absolute_url())
> > >
> > >
> > > im actually experiencing problems with the edit form
> where i can
> > > get the
> > > values displayed in the form if i want to edit but i get a
> > >
> > >Exception Type: MultiValueDictKeyError
> > >Exception Value:
> > >
> > >Key 'activity_set-0-id' not found in  > > {u'activity_set-0-name': [u'a\xf6lskjdf\xf6sa'],
> > > u'activity_set-0-type': [u'research']
> > >
> > > on save... any hints?
> > >
> > >
> > > Stefan Nitsche wrote:
> > > > On Mon, Jan 4, 2010 at 23:27, Stefan Nitsche
> > 
> >
> > > 
> >>
> > > > 
> >
> > 
>  wrote:
> > > >
> > > > Hi,
> > > >
> > > > to begin with I would like to declare that I'm still
> > on the
> > > > beginner end of the scale when it comes to
> Python and
> > Django.
> > > >
> > > > I have an app which has two models:
> > > >
> > > > class Item(models.Model):
> > > > title = models.CharField()
> > > > description_markdown = models.TextField()
> > > >
> > > > class ItemImage(models.Model):
> > > > item = models.ForeignKey(Item)
> > > > image = models.ImageField(upload_to='tmp')
> > > >
> > > > What I want to do now is to create a page where
> users
> > can submit
> > > > an item and upload an image (or images) at the same
> > time. I can
> > > > create a form using ModelForm for the Item-model
> and I can
> > > create
> > > > a form for the ItemImage-model using
> > inlineformset_factory, if I
> > > > do this the submit page looks like it should.
> However
> > it doesn't
> > > > behave the way I want it to when saving, but to be
> > honest I have
> > > > no real idea of what I'm doing when it comes to the
> > related
> > > > model/form.
> > > >
> > > > If I understand it correctly when using
> > inlineformset_factory I
> > > > must give it an instance so that it can map the
> > foreignkey,
> > > > correct? So how do one go about and create a
> form where
> > > people can
> > 

Re: Models, forms and inlineformset

2010-01-30 Thread Stefan Nitsche
On Sat, Jan 30, 2010 at 11:23, andreas schmid  wrote:

> Stefan Nitsche wrote:
> >
> >
> > On Thu, Jan 28, 2010 at 10:15, andreas schmid  > > wrote:
> >
> > and your form works on edit too?
> > i really cant understand why mine isnt...
> >
> > its giving me this MultiValueDictKeyError which i dont understand.
> >
> > Stefan Nitsche wrote:
> > > On Wed, Jan 27, 2010 at 14:04, andreas schmid
> > 
> > > >>
> wrote:
> > >
> > > is this an add or an edit form? or both?
> > >
> > > i have a similar inlineformset and i need to loop over the
> > inline
> > > objects to set the foreignkey to the parent object like:
> > >
> > >if form.is_valid() and formset.is_valid(): # All validation
> > > rules pass
> > >new_idea = form.save(commit=False)
> > >new_idea.author = request.user
> > >new_idea = form.save()
> > >formset_models = formset.save(commit=False)
> > >for f in formset_models:
> > >f.idea = new_idea
> > >f.save()
> > >return
> > > HttpResponseRedirect(new_idea.get_absolute_url())
> > >
> > >
> > > im actually experiencing problems with the edit form where i
> can
> > > get the
> > > values displayed in the form if i want to edit but i get a
> > >
> > >Exception Type: MultiValueDictKeyError
> > >Exception Value:
> > >
> > >Key 'activity_set-0-id' not found in  > > {u'activity_set-0-name': [u'a\xf6lskjdf\xf6sa'],
> > > u'activity_set-0-type': [u'research']
> > >
> > > on save... any hints?
> > >
> > >
> > > Stefan Nitsche wrote:
> > > > On Mon, Jan 4, 2010 at 23:27, Stefan Nitsche
> > 
> > > >
> > > > 
> >  > > >
> > > > Hi,
> > > >
> > > > to begin with I would like to declare that I'm still
> > on the
> > > > beginner end of the scale when it comes to Python and
> > Django.
> > > >
> > > > I have an app which has two models:
> > > >
> > > > class Item(models.Model):
> > > > title = models.CharField()
> > > > description_markdown = models.TextField()
> > > >
> > > > class ItemImage(models.Model):
> > > > item = models.ForeignKey(Item)
> > > > image = models.ImageField(upload_to='tmp')
> > > >
> > > > What I want to do now is to create a page where users
> > can submit
> > > > an item and upload an image (or images) at the same
> > time. I can
> > > > create a form using ModelForm for the Item-model and I
> can
> > > create
> > > > a form for the ItemImage-model using
> > inlineformset_factory, if I
> > > > do this the submit page looks like it should. However
> > it doesn't
> > > > behave the way I want it to when saving, but to be
> > honest I have
> > > > no real idea of what I'm doing when it comes to the
> > related
> > > > model/form.
> > > >
> > > > If I understand it correctly when using
> > inlineformset_factory I
> > > > must give it an instance so that it can map the
> > foreignkey,
> > > > correct? So how do one go about and create a form where
> > > people can
> > > > add "item" and "itemimages" at the same time? I'm
> > feeling totaly
> > > > lost any pointers would be greatly appreciated.
> > > >
> > > > --
> > > > Stefan Nitsche
> > > > ste...@nitsche.se 
> > >
> > > 
> > >>
> > > >
> > > >
> > > >
> > > > Ok I'm totally lost here. I've created an edit function
> > which works
> > > > splendidly except for the inline-part, it displays alright
> > but it
> > > > isn't saved at all.
> > > >
> > > > My models look like this:
> > > > ==
> > > >
> > > > class Recipe(models.Model):
> > > > author = 

Re: Models, forms and inlineformset

2010-01-30 Thread andreas schmid
Stefan Nitsche wrote:
>
>
> On Thu, Jan 28, 2010 at 10:15, andreas schmid  > wrote:
>
> and your form works on edit too?
> i really cant understand why mine isnt...
>
> its giving me this MultiValueDictKeyError which i dont understand.
>
> Stefan Nitsche wrote:
> > On Wed, Jan 27, 2010 at 14:04, andreas schmid
> 
> > >> wrote:
> >
> > is this an add or an edit form? or both?
> >
> > i have a similar inlineformset and i need to loop over the
> inline
> > objects to set the foreignkey to the parent object like:
> >
> >if form.is_valid() and formset.is_valid(): # All validation
> > rules pass
> >new_idea = form.save(commit=False)
> >new_idea.author = request.user
> >new_idea = form.save()
> >formset_models = formset.save(commit=False)
> >for f in formset_models:
> >f.idea = new_idea
> >f.save()
> >return
> > HttpResponseRedirect(new_idea.get_absolute_url())
> >
> >
> > im actually experiencing problems with the edit form where i can
> > get the
> > values displayed in the form if i want to edit but i get a
> >
> >Exception Type: MultiValueDictKeyError
> >Exception Value:
> >
> >Key 'activity_set-0-id' not found in  > {u'activity_set-0-name': [u'a\xf6lskjdf\xf6sa'],
> > u'activity_set-0-type': [u'research']
> >
> > on save... any hints?
> >
> >
> > Stefan Nitsche wrote:
> > > On Mon, Jan 4, 2010 at 23:27, Stefan Nitsche
> 
> > >
> > > 
>  > >
> > > Hi,
> > >
> > > to begin with I would like to declare that I'm still
> on the
> > > beginner end of the scale when it comes to Python and
> Django.
> > >
> > > I have an app which has two models:
> > >
> > > class Item(models.Model):
> > > title = models.CharField()
> > > description_markdown = models.TextField()
> > >
> > > class ItemImage(models.Model):
> > > item = models.ForeignKey(Item)
> > > image = models.ImageField(upload_to='tmp')
> > >
> > > What I want to do now is to create a page where users
> can submit
> > > an item and upload an image (or images) at the same
> time. I can
> > > create a form using ModelForm for the Item-model and I can
> > create
> > > a form for the ItemImage-model using
> inlineformset_factory, if I
> > > do this the submit page looks like it should. However
> it doesn't
> > > behave the way I want it to when saving, but to be
> honest I have
> > > no real idea of what I'm doing when it comes to the
> related
> > > model/form.
> > >
> > > If I understand it correctly when using
> inlineformset_factory I
> > > must give it an instance so that it can map the
> foreignkey,
> > > correct? So how do one go about and create a form where
> > people can
> > > add "item" and "itemimages" at the same time? I'm
> feeling totaly
> > > lost any pointers would be greatly appreciated.
> > >
> > > --
> > > Stefan Nitsche
> > > ste...@nitsche.se 
> >
> > 
> >>
> > >
> > >
> > >
> > > Ok I'm totally lost here. I've created an edit function
> which works
> > > splendidly except for the inline-part, it displays alright
> but it
> > > isn't saved at all.
> > >
> > > My models look like this:
> > > ==
> > >
> > > class Recipe(models.Model):
> > > author = models.ForeignKey(User)
> > > name = models.CharField(max_length=255, verbose_name='Namn
> > på rätten')
> > > category = models.ForeignKey('Category',
> > verbose_name='Kategori')
> > > create_date = models.DateTimeField(auto_now_add=True)
> > > servings = 

Re: Models, forms and inlineformset

2010-01-29 Thread Stefan Nitsche
On Thu, Jan 28, 2010 at 10:15, andreas schmid  wrote:

> and your form works on edit too?
> i really cant understand why mine isnt...
>
> its giving me this MultiValueDictKeyError which i dont understand.
>
> Stefan Nitsche wrote:
> > On Wed, Jan 27, 2010 at 14:04, andreas schmid  > > wrote:
> >
> > is this an add or an edit form? or both?
> >
> > i have a similar inlineformset and i need to loop over the inline
> > objects to set the foreignkey to the parent object like:
> >
> >if form.is_valid() and formset.is_valid(): # All validation
> > rules pass
> >new_idea = form.save(commit=False)
> >new_idea.author = request.user
> >new_idea = form.save()
> >formset_models = formset.save(commit=False)
> >for f in formset_models:
> >f.idea = new_idea
> >f.save()
> >return
> > HttpResponseRedirect(new_idea.get_absolute_url())
> >
> >
> > im actually experiencing problems with the edit form where i can
> > get the
> > values displayed in the form if i want to edit but i get a
> >
> >Exception Type: MultiValueDictKeyError
> >Exception Value:
> >
> >Key 'activity_set-0-id' not found in  > {u'activity_set-0-name': [u'a\xf6lskjdf\xf6sa'],
> > u'activity_set-0-type': [u'research']
> >
> > on save... any hints?
> >
> >
> > Stefan Nitsche wrote:
> > > On Mon, Jan 4, 2010 at 23:27, Stefan Nitsche  > 
> > > >> wrote:
> > >
> > > Hi,
> > >
> > > to begin with I would like to declare that I'm still on the
> > > beginner end of the scale when it comes to Python and Django.
> > >
> > > I have an app which has two models:
> > >
> > > class Item(models.Model):
> > > title = models.CharField()
> > > description_markdown = models.TextField()
> > >
> > > class ItemImage(models.Model):
> > > item = models.ForeignKey(Item)
> > > image = models.ImageField(upload_to='tmp')
> > >
> > > What I want to do now is to create a page where users can
> submit
> > > an item and upload an image (or images) at the same time. I can
> > > create a form using ModelForm for the Item-model and I can
> > create
> > > a form for the ItemImage-model using inlineformset_factory, if
> I
> > > do this the submit page looks like it should. However it
> doesn't
> > > behave the way I want it to when saving, but to be honest I
> have
> > > no real idea of what I'm doing when it comes to the related
> > > model/form.
> > >
> > > If I understand it correctly when using inlineformset_factory I
> > > must give it an instance so that it can map the foreignkey,
> > > correct? So how do one go about and create a form where
> > people can
> > > add "item" and "itemimages" at the same time? I'm feeling
> totaly
> > > lost any pointers would be greatly appreciated.
> > >
> > > --
> > > Stefan Nitsche
> > > ste...@nitsche.se 
> > >
> > >
> > >
> > >
> > > Ok I'm totally lost here. I've created an edit function which works
> > > splendidly except for the inline-part, it displays alright but it
> > > isn't saved at all.
> > >
> > > My models look like this:
> > > ==
> > >
> > > class Recipe(models.Model):
> > > author = models.ForeignKey(User)
> > > name = models.CharField(max_length=255, verbose_name='Namn
> > på rätten')
> > > category = models.ForeignKey('Category',
> > verbose_name='Kategori')
> > > create_date = models.DateTimeField(auto_now_add=True)
> > > servings = models.CharField(max_length=10,
> > verbose_name='Portioner')
> > > cooking_time = models.CharField(max_length=10,
> > > verbose_name='Tillagningstid')
> > > ingredients = models.TextField(verbose_name='Ingridienser')
> > > instructions = models.TextField(verbose_name='Instruktioner')
> > > oven_heat = models.CharField(max_length=10, null=True,
> > blank=True,
> > > verbose_name='Ugnsvärme')
> > > serving_tips = models.TextField(null=True, blank=True,
> > > verbose_name='Serveringsförslag')
> > > tags = TagField(verbose_name='Taggar')
> > > slug = models.SlugField()
> > > published = models.NullBooleanField(default=1)
> > >
> > > class Image(models.Model):
> > > recipe = models.ForeignKey(Recipe)
> > > file = 

Re: Models, forms and inlineformset

2010-01-28 Thread andreas schmid
i forgot to add my edit view:

@login_required
def edit_projectidea(request, slug):
projectidea = get_object_or_404(Projectidea, slug=slug)
activity_formset = inlineformset_factory(Projectidea, Activity, extra=7)
form = ProjectideaForm(instance=projectidea)
formset = activity_formset(instance=projectidea)
if request.user.id != projectidea.author.id:
return HttpResponseForbidden()
if request.method == 'POST':
form = ProjectideaForm(request.POST, instance=projectidea)
formset = activity_formset(request.POST, instance=projectidea)
if form.is_valid() and formset.is_valid(): # All validation
rules pass
new_idea = form.save(commit=False)
new_idea.author = request.user
new_idea = form.save()
formset_models = formset.save(commit=False)
for f in formset_models:
f.projectidea = new_idea
f.save()
return HttpResponseRedirect(new_idea.get_absolute_url()) #
Redirect after POST
else:
form = ProjectideaForm(request.POST, instance=projectidea)
formset = activity_formset(request.POST, instance=projectidea)
return render_to_response('fslform/fslform.html',
  { 'form': form, 'formset': formset ,'add':
False },
  context_instance=RequestContext(request))


andreas schmid wrote:
> and your form works on edit too?
> i really cant understand why mine isnt...
>
> its giving me this MultiValueDictKeyError which i dont understand.
>
> Stefan Nitsche wrote:
>   
>> On Wed, Jan 27, 2010 at 14:04, andreas schmid > > wrote:
>>
>> is this an add or an edit form? or both?
>>
>> i have a similar inlineformset and i need to loop over the inline
>> objects to set the foreignkey to the parent object like:
>>
>>if form.is_valid() and formset.is_valid(): # All validation
>> rules pass
>>new_idea = form.save(commit=False)
>>new_idea.author = request.user
>>new_idea = form.save()
>>formset_models = formset.save(commit=False)
>>for f in formset_models:
>>f.idea = new_idea
>>f.save()
>>return
>> HttpResponseRedirect(new_idea.get_absolute_url())
>>
>>
>> im actually experiencing problems with the edit form where i can
>> get the
>> values displayed in the form if i want to edit but i get a
>>
>>Exception Type: MultiValueDictKeyError
>>Exception Value:
>>
>>Key 'activity_set-0-id' not found in > {u'activity_set-0-name': [u'a\xf6lskjdf\xf6sa'],
>> u'activity_set-0-type': [u'research']
>>
>> on save... any hints?
>>
>>
>> Stefan Nitsche wrote:
>> > On Mon, Jan 4, 2010 at 23:27, Stefan Nitsche > 
>> > >> wrote:
>> >
>> > Hi,
>> >
>> > to begin with I would like to declare that I'm still on the
>> > beginner end of the scale when it comes to Python and Django.
>> >
>> > I have an app which has two models:
>> >
>> > class Item(models.Model):
>> > title = models.CharField()
>> > description_markdown = models.TextField()
>> >
>> > class ItemImage(models.Model):
>> > item = models.ForeignKey(Item)
>> > image = models.ImageField(upload_to='tmp')
>> >
>> > What I want to do now is to create a page where users can submit
>> > an item and upload an image (or images) at the same time. I can
>> > create a form using ModelForm for the Item-model and I can
>> create
>> > a form for the ItemImage-model using inlineformset_factory, if I
>> > do this the submit page looks like it should. However it doesn't
>> > behave the way I want it to when saving, but to be honest I have
>> > no real idea of what I'm doing when it comes to the related
>> > model/form.
>> >
>> > If I understand it correctly when using inlineformset_factory I
>> > must give it an instance so that it can map the foreignkey,
>> > correct? So how do one go about and create a form where
>> people can
>> > add "item" and "itemimages" at the same time? I'm feeling totaly
>> > lost any pointers would be greatly appreciated.
>> >
>> > --
>> > Stefan Nitsche
>> > ste...@nitsche.se 
>> >
>> >
>> >
>> >
>> > Ok I'm totally lost here. I've created an edit function which works
>> > splendidly except for the inline-part, it displays alright but it
>> > isn't saved at all.
>> >
>> > My 

Re: Models, forms and inlineformset

2010-01-28 Thread andreas schmid
and your form works on edit too?
i really cant understand why mine isnt...

its giving me this MultiValueDictKeyError which i dont understand.

Stefan Nitsche wrote:
> On Wed, Jan 27, 2010 at 14:04, andreas schmid  > wrote:
>
> is this an add or an edit form? or both?
>
> i have a similar inlineformset and i need to loop over the inline
> objects to set the foreignkey to the parent object like:
>
>if form.is_valid() and formset.is_valid(): # All validation
> rules pass
>new_idea = form.save(commit=False)
>new_idea.author = request.user
>new_idea = form.save()
>formset_models = formset.save(commit=False)
>for f in formset_models:
>f.idea = new_idea
>f.save()
>return
> HttpResponseRedirect(new_idea.get_absolute_url())
>
>
> im actually experiencing problems with the edit form where i can
> get the
> values displayed in the form if i want to edit but i get a
>
>Exception Type: MultiValueDictKeyError
>Exception Value:
>
>Key 'activity_set-0-id' not found in  {u'activity_set-0-name': [u'a\xf6lskjdf\xf6sa'],
> u'activity_set-0-type': [u'research']
>
> on save... any hints?
>
>
> Stefan Nitsche wrote:
> > On Mon, Jan 4, 2010 at 23:27, Stefan Nitsche  
> > >> wrote:
> >
> > Hi,
> >
> > to begin with I would like to declare that I'm still on the
> > beginner end of the scale when it comes to Python and Django.
> >
> > I have an app which has two models:
> >
> > class Item(models.Model):
> > title = models.CharField()
> > description_markdown = models.TextField()
> >
> > class ItemImage(models.Model):
> > item = models.ForeignKey(Item)
> > image = models.ImageField(upload_to='tmp')
> >
> > What I want to do now is to create a page where users can submit
> > an item and upload an image (or images) at the same time. I can
> > create a form using ModelForm for the Item-model and I can
> create
> > a form for the ItemImage-model using inlineformset_factory, if I
> > do this the submit page looks like it should. However it doesn't
> > behave the way I want it to when saving, but to be honest I have
> > no real idea of what I'm doing when it comes to the related
> > model/form.
> >
> > If I understand it correctly when using inlineformset_factory I
> > must give it an instance so that it can map the foreignkey,
> > correct? So how do one go about and create a form where
> people can
> > add "item" and "itemimages" at the same time? I'm feeling totaly
> > lost any pointers would be greatly appreciated.
> >
> > --
> > Stefan Nitsche
> > ste...@nitsche.se 
> >
> >
> >
> >
> > Ok I'm totally lost here. I've created an edit function which works
> > splendidly except for the inline-part, it displays alright but it
> > isn't saved at all.
> >
> > My models look like this:
> > ==
> >
> > class Recipe(models.Model):
> > author = models.ForeignKey(User)
> > name = models.CharField(max_length=255, verbose_name='Namn
> på rätten')
> > category = models.ForeignKey('Category',
> verbose_name='Kategori')
> > create_date = models.DateTimeField(auto_now_add=True)
> > servings = models.CharField(max_length=10,
> verbose_name='Portioner')
> > cooking_time = models.CharField(max_length=10,
> > verbose_name='Tillagningstid')
> > ingredients = models.TextField(verbose_name='Ingridienser')
> > instructions = models.TextField(verbose_name='Instruktioner')
> > oven_heat = models.CharField(max_length=10, null=True,
> blank=True,
> > verbose_name='Ugnsvärme')
> > serving_tips = models.TextField(null=True, blank=True,
> > verbose_name='Serveringsförslag')
> > tags = TagField(verbose_name='Taggar')
> > slug = models.SlugField()
> > published = models.NullBooleanField(default=1)
> >
> > class Image(models.Model):
> > recipe = models.ForeignKey(Recipe)
> > file = models.ImageField(upload_to=get_file_path)
> >
> > My view looks like this:
> > =
> > from django.shortcuts import render_to_response, get_object_or_404
> > from django.template import RequestContext
> > from django.http import HttpResponseRedirect
> > from nitsche.recipes.models import 

Re: Models, forms and inlineformset

2010-01-27 Thread Stefan Nitsche
On Wed, Jan 27, 2010 at 14:04, andreas schmid  wrote:

> is this an add or an edit form? or both?
>
> i have a similar inlineformset and i need to loop over the inline
> objects to set the foreignkey to the parent object like:
>
>if form.is_valid() and formset.is_valid(): # All validation rules pass
>new_idea = form.save(commit=False)
>new_idea.author = request.user
>new_idea = form.save()
>formset_models = formset.save(commit=False)
>for f in formset_models:
>f.idea = new_idea
>f.save()
>return HttpResponseRedirect(new_idea.get_absolute_url())
>
>
> im actually experiencing problems with the edit form where i can get the
> values displayed in the form if i want to edit but i get a
>
>Exception Type: MultiValueDictKeyError
>Exception Value:
>
>Key 'activity_set-0-id' not found in  {u'activity_set-0-name': [u'a\xf6lskjdf\xf6sa'], u'activity_set-0-type':
> [u'research']
>
> on save... any hints?
>
>
> Stefan Nitsche wrote:
> > On Mon, Jan 4, 2010 at 23:27, Stefan Nitsche  > > wrote:
> >
> > Hi,
> >
> > to begin with I would like to declare that I'm still on the
> > beginner end of the scale when it comes to Python and Django.
> >
> > I have an app which has two models:
> >
> > class Item(models.Model):
> > title = models.CharField()
> > description_markdown = models.TextField()
> >
> > class ItemImage(models.Model):
> > item = models.ForeignKey(Item)
> > image = models.ImageField(upload_to='tmp')
> >
> > What I want to do now is to create a page where users can submit
> > an item and upload an image (or images) at the same time. I can
> > create a form using ModelForm for the Item-model and I can create
> > a form for the ItemImage-model using inlineformset_factory, if I
> > do this the submit page looks like it should. However it doesn't
> > behave the way I want it to when saving, but to be honest I have
> > no real idea of what I'm doing when it comes to the related
> > model/form.
> >
> > If I understand it correctly when using inlineformset_factory I
> > must give it an instance so that it can map the foreignkey,
> > correct? So how do one go about and create a form where people can
> > add "item" and "itemimages" at the same time? I'm feeling totaly
> > lost any pointers would be greatly appreciated.
> >
> > --
> > Stefan Nitsche
> > ste...@nitsche.se 
> >
> >
> >
> > Ok I'm totally lost here. I've created an edit function which works
> > splendidly except for the inline-part, it displays alright but it
> > isn't saved at all.
> >
> > My models look like this:
> > ==
> >
> > class Recipe(models.Model):
> > author = models.ForeignKey(User)
> > name = models.CharField(max_length=255, verbose_name='Namn på
> rätten')
> > category = models.ForeignKey('Category', verbose_name='Kategori')
> > create_date = models.DateTimeField(auto_now_add=True)
> > servings = models.CharField(max_length=10, verbose_name='Portioner')
> > cooking_time = models.CharField(max_length=10,
> > verbose_name='Tillagningstid')
> > ingredients = models.TextField(verbose_name='Ingridienser')
> > instructions = models.TextField(verbose_name='Instruktioner')
> > oven_heat = models.CharField(max_length=10, null=True, blank=True,
> > verbose_name='Ugnsvärme')
> > serving_tips = models.TextField(null=True, blank=True,
> > verbose_name='Serveringsförslag')
> > tags = TagField(verbose_name='Taggar')
> > slug = models.SlugField()
> > published = models.NullBooleanField(default=1)
> >
> > class Image(models.Model):
> > recipe = models.ForeignKey(Recipe)
> > file = models.ImageField(upload_to=get_file_path)
> >
> > My view looks like this:
> > =
> > from django.shortcuts import render_to_response, get_object_or_404
> > from django.template import RequestContext
> > from django.http import HttpResponseRedirect
> > from nitsche.recipes.models import Category, Recipe, Image
> > from nitsche.recipes.forms import RecipeForm
> > from django.contrib.auth.decorators import login_required
> > from django.forms.models import inlineformset_factory
> >
> > @login_required
> > def edit(request, slug):
> > ImageFormSet = inlineformset_factory(Recipe, Image)
> > recipe = Recipe.objects.filter(published=True).get(slug=slug)
> > form = RecipeForm(instance=recipe)
> > formset = ImageFormSet(instance=recipe)
> > if request.method == 'POST':
> > form = RecipeForm(request.POST, instance=recipe)
> > formset = ImageFormSet(request.POST, request.FILES,
> > instance=recipe)
> > if form.is_valid(): # All validation rules pass
> > form.save()
> > if 

Re: Models, forms and inlineformset

2010-01-27 Thread andreas schmid
is this an add or an edit form? or both?

i have a similar inlineformset and i need to loop over the inline
objects to set the foreignkey to the parent object like:

if form.is_valid() and formset.is_valid(): # All validation rules pass
new_idea = form.save(commit=False)
new_idea.author = request.user
new_idea = form.save()
formset_models = formset.save(commit=False)
for f in formset_models:
f.idea = new_idea
f.save()
return HttpResponseRedirect(new_idea.get_absolute_url())


im actually experiencing problems with the edit form where i can get the
values displayed in the form if i want to edit but i get a

Exception Type: MultiValueDictKeyError
Exception Value:

Key 'activity_set-0-id' not found in  On Mon, Jan 4, 2010 at 23:27, Stefan Nitsche  > wrote:
>
> Hi,
>
> to begin with I would like to declare that I'm still on the
> beginner end of the scale when it comes to Python and Django.
>
> I have an app which has two models:
>
> class Item(models.Model):
> title = models.CharField()
> description_markdown = models.TextField()
>
> class ItemImage(models.Model):
> item = models.ForeignKey(Item)
> image = models.ImageField(upload_to='tmp')
>
> What I want to do now is to create a page where users can submit
> an item and upload an image (or images) at the same time. I can
> create a form using ModelForm for the Item-model and I can create
> a form for the ItemImage-model using inlineformset_factory, if I
> do this the submit page looks like it should. However it doesn't
> behave the way I want it to when saving, but to be honest I have
> no real idea of what I'm doing when it comes to the related
> model/form.
>
> If I understand it correctly when using inlineformset_factory I
> must give it an instance so that it can map the foreignkey,
> correct? So how do one go about and create a form where people can
> add "item" and "itemimages" at the same time? I'm feeling totaly
> lost any pointers would be greatly appreciated.
>
> -- 
> Stefan Nitsche
> ste...@nitsche.se 
>
>
>
> Ok I'm totally lost here. I've created an edit function which works
> splendidly except for the inline-part, it displays alright but it
> isn't saved at all.
>
> My models look like this:
> ==
>
> class Recipe(models.Model): 
> author = models.ForeignKey(User)
> name = models.CharField(max_length=255, verbose_name='Namn på rätten')
> category = models.ForeignKey('Category', verbose_name='Kategori')
> create_date = models.DateTimeField(auto_now_add=True)
> servings = models.CharField(max_length=10, verbose_name='Portioner')
> cooking_time = models.CharField(max_length=10,
> verbose_name='Tillagningstid')
> ingredients = models.TextField(verbose_name='Ingridienser')
> instructions = models.TextField(verbose_name='Instruktioner')
> oven_heat = models.CharField(max_length=10, null=True, blank=True,
> verbose_name='Ugnsvärme')
> serving_tips = models.TextField(null=True, blank=True,
> verbose_name='Serveringsförslag')
> tags = TagField(verbose_name='Taggar')
> slug = models.SlugField()
> published = models.NullBooleanField(default=1)
>
> class Image(models.Model):
> recipe = models.ForeignKey(Recipe)
> file = models.ImageField(upload_to=get_file_path)
>
> My view looks like this:
> =
> from django.shortcuts import render_to_response, get_object_or_404
> from django.template import RequestContext
> from django.http import HttpResponseRedirect
> from nitsche.recipes.models import Category, Recipe, Image
> from nitsche.recipes.forms import RecipeForm
> from django.contrib.auth.decorators import login_required
> from django.forms.models import inlineformset_factory
>
> @login_required
> def edit(request, slug):
> ImageFormSet = inlineformset_factory(Recipe, Image)
> recipe = Recipe.objects.filter(published=True).get(slug=slug)
> form = RecipeForm(instance=recipe)
> formset = ImageFormSet(instance=recipe)
> if request.method == 'POST':
> form = RecipeForm(request.POST, instance=recipe)
> formset = ImageFormSet(request.POST, request.FILES,
> instance=recipe)
> if form.is_valid(): # All validation rules pass
> form.save()
> if formset.is_valid():
> formset.save() 
> return HttpResponseRedirect('/recept')
> return render_to_response('recipes/add.html',
>   {'form': form, 'formset': formset,
> 'slug':slug,},
>   context_instance=RequestContext(request))
>
> And finaly my template looks like this:
> 
>
> 
> {{ form.as_p }}
> 
> {{ 

Re: Models, forms and inlineformset

2010-01-23 Thread Stefan Nitsche
On Mon, Jan 4, 2010 at 23:27, Stefan Nitsche  wrote:

> Hi,
>
> to begin with I would like to declare that I'm still on the beginner end of
> the scale when it comes to Python and Django.
>
> I have an app which has two models:
>
> class Item(models.Model):
> title = models.CharField()
> description_markdown = models.TextField()
>
> class ItemImage(models.Model):
> item = models.ForeignKey(Item)
> image = models.ImageField(upload_to='tmp')
>
> What I want to do now is to create a page where users can submit an item
> and upload an image (or images) at the same time. I can create a form using
> ModelForm for the Item-model and I can create a form for the ItemImage-model
> using inlineformset_factory, if I do this the submit page looks like it
> should. However it doesn't behave the way I want it to when saving, but to
> be honest I have no real idea of what I'm doing when it comes to the related
> model/form.
>
> If I understand it correctly when using inlineformset_factory I must give
> it an instance so that it can map the foreignkey, correct? So how do one go
> about and create a form where people can add "item" and "itemimages" at the
> same time? I'm feeling totaly lost any pointers would be greatly
> appreciated.
>
> --
> Stefan Nitsche
> ste...@nitsche.se
>


Ok I'm totally lost here. I've created an edit function which works
splendidly except for the inline-part, it displays alright but it isn't
saved at all.

My models look like this:
==

class Recipe(models.Model):
author = models.ForeignKey(User)
name = models.CharField(max_length=255, verbose_name='Namn på rätten')
category = models.ForeignKey('Category', verbose_name='Kategori')
create_date = models.DateTimeField(auto_now_add=True)
servings = models.CharField(max_length=10, verbose_name='Portioner')
cooking_time = models.CharField(max_length=10,
verbose_name='Tillagningstid')
ingredients = models.TextField(verbose_name='Ingridienser')
instructions = models.TextField(verbose_name='Instruktioner')
oven_heat = models.CharField(max_length=10, null=True, blank=True,
verbose_name='Ugnsvärme')
serving_tips = models.TextField(null=True, blank=True,
verbose_name='Serveringsförslag')
tags = TagField(verbose_name='Taggar')
slug = models.SlugField()
published = models.NullBooleanField(default=1)

class Image(models.Model):
recipe = models.ForeignKey(Recipe)
file = models.ImageField(upload_to=get_file_path)

My view looks like this:
=
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponseRedirect
from nitsche.recipes.models import Category, Recipe, Image
from nitsche.recipes.forms import RecipeForm
from django.contrib.auth.decorators import login_required
from django.forms.models import inlineformset_factory

@login_required
def edit(request, slug):
ImageFormSet = inlineformset_factory(Recipe, Image)
recipe = Recipe.objects.filter(published=True).get(slug=slug)
form = RecipeForm(instance=recipe)
formset = ImageFormSet(instance=recipe)
if request.method == 'POST':
form = RecipeForm(request.POST, instance=recipe)
formset = ImageFormSet(request.POST, request.FILES, instance=recipe)
if form.is_valid(): # All validation rules pass
form.save()
if formset.is_valid():
formset.save()
return HttpResponseRedirect('/recept')
return render_to_response('recipes/add.html',
  {'form': form, 'formset': formset,
'slug':slug,},
  context_instance=RequestContext(request))

And finaly my template looks like this:



{{ form.as_p }}

{{ formset }}




Every change I make in the main form (for the recipe-model) is saved but
nothing from the inline_formset (the image-model). I get no error or
anything, it behaves as if it is saving all the data but in the reality
nothing in the inline_formset is saved. I've read the documentation and as
far as I understand it should work this way. When I add or edit an item in
the backend it works as it should. Can somebody point out what I'm doing
wrong? At this point I am starting to feel a little desperate and any nudge
in the right direction would be greatly appreciated.

-- 
Stefan Nitsche
ste...@nitsche.se

-- 
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.



Models, forms and inlineformset

2010-01-04 Thread Stefan Nitsche
Hi,

to begin with I would like to declare that I'm still on the beginner end of
the scale when it comes to Python and Django.

I have an app which has two models:

class Item(models.Model):
title = models.CharField()
description_markdown = models.TextField()

class ItemImage(models.Model):
item = models.ForeignKey(Item)
image = models.ImageField(upload_to='tmp')

What I want to do now is to create a page where users can submit an item and
upload an image (or images) at the same time. I can create a form using
ModelForm for the Item-model and I can create a form for the ItemImage-model
using inlineformset_factory, if I do this the submit page looks like it
should. However it doesn't behave the way I want it to when saving, but to
be honest I have no real idea of what I'm doing when it comes to the related
model/form.

If I understand it correctly when using inlineformset_factory I must give it
an instance so that it can map the foreignkey, correct? So how do one go
about and create a form where people can add "item" and "itemimages" at the
same time? I'm feeling totaly lost any pointers would be greatly
appreciated.

-- 
Stefan Nitsche
ste...@nitsche.se

--

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.