Could you please paste your form_valid method ?
 You should update it for right saving object

Many thanks,

Serge


+380 636150445
skype: skhohlov

On Thu, Oct 9, 2014 at 7:40 AM, Lachlan Musicman <data...@gmail.com> wrote:

> Ok, I think it's because the modela is not passed in the POST data. Is
> there any way to set the modela field in the CreateView without
> needing it to be in the POST data?
>
> IE, set the field in the CreateView (because the value is known
> immediately by virtue of <slug>), and leave it out of the ModelForm
> and the template?
>
> Cheers
> L.
>
> On 9 October 2014 15:33, Lachlan Musicman <data...@gmail.com> wrote:
> > I'm struggling with CreateView get_initial() in django1.7
> >
> > I have a modelA detail page with an "add ModelB" (modelB has FK to
> ModelA)
> > button.
> >
> > When I hit that button, I want the new ModelB obj to have the modelA FK
> set. So
> > in the ModelB CreateView I have a get_initial that uses the slug to set
> the
> > modela.
> >
> > When I run the code below , the modelB wont save ("missing modelA_id")
> >
> > forms.py doesn't include modela field because I want modela set without
> ability
> > to edit it. Have tried including it, and excluding from the template -
> always
> > get's a "modela is required field" error.
> >
> > What am I doing wrong or what test can I run to determine why the
> > field isn't being set?
> >
> >
> > models.py:
> >
> > class ModelA(models.Model):
> >   name = models.CharField(max_length=30)
> >
> > class ModelB(models.Model):
> >   name = models.CharField(max_length=30)
> >   modela = models.ForeignKey(ModelA, related_name='moda')
> >
> >
> > urls.py:
> >
> > url(r'^model/add/$', views.ModelAAdd.as_view(), name='modelA_add'),
> > url(r'^model/(?P<slug>[-\w]+)/$', views.ModelADetail.as_view(),
> > name='modelA_detail'),
> > url(r'^model/(?P<slug>[-\w]+)/modelb/add/$',
> > views.ModelBAdd.as_view(), name='modelB_add'),
> >
> >
> > views.py:
> >
> > class ModelBAdd(generic.CreateView):
> >   fields = ['modela', 'name']
> >   form_class = ModelBAddForm
> >   model = ModelB
> >   template_name = 'jobs/modelB_form.html'
> >
> >   def get_initial(self):
> >     slug = self.kwargs['slug']
> >     modela = ModelA.objects.get(slug=slug)
> >     print modela.id, modela  # <- prints correct details in log
> >     return {'modela': modela.id }
> >
> > forms.py
> >
> > class ModelBAddForm(forms.ModelForm):
> >   class Meta:
> >     fields = ['name']
> >     model = ModelB
> >     widgets = {
> >             'name': forms.TextInput(attrs={'class':'form-control'}),
> >     }
> >
> >
> >
> > --
> > You have to be really clever to come up with a genuinely dangerous
> > thought. I am disheartened that people can be clever enough to do that
> > and not clever enough to do the obvious thing and KEEP THEIR IDIOT
> > MOUTHS SHUT about it, because it is much more important to sound
> > intelligent when talking to your friends.
> > This post was STUPID.
> >
> -----------------------------------------------------------------------------------------------------------
> > The Most Terrifying Thought Experiment of All Time
> >
> http://www.slate.com/articles/technology/bitwise/2014/07/roko_s_basilisk_the_most_terrifying_thought_experiment_of_all_time.html
>
>
>
> --
> You have to be really clever to come up with a genuinely dangerous
> thought. I am disheartened that people can be clever enough to do that
> and not clever enough to do the obvious thing and KEEP THEIR IDIOT
> MOUTHS SHUT about it, because it is much more important to sound
> intelligent when talking to your friends.
> This post was STUPID.
>
> -----------------------------------------------------------------------------------------------------------
> The Most Terrifying Thought Experiment of All Time
>
> http://www.slate.com/articles/technology/bitwise/2014/07/roko_s_basilisk_the_most_terrifying_thought_experiment_of_all_time.html
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAGBeqiMB0EnD94STioijH4iL2V1xvX4fxZkm9beoPmxs3JWPtw%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADTRxJOQDaJ9bh7fRZOqW-BqhtHR_n%2B-jRsrN9dFVMQngX1r8g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to