caveat - I have never used generic views, class based or otherwise, but
I was under the impression that if one wants customised forms or views,
generic is not the way to go.
--
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/
Why not? The model edit views use the ModelFormMixin, which has a
get_form_class that can be overriden [1] to taste. You can simply get
the form from super().get_form_class and put it through
modelform_factory [2] again to only get the form for the fields you
want.
The classy generic views are really flexible like that. Sadly docs
aren't that great, so reading the code is probably best to get the hang
of it.
[1]
https://code.djangoproject.com/browser/django/trunk/django/views/generic/edit.py#L66
[2]
https://code.djangoproject.com/browser/django/trunk/django/forms/models.py#L370
Cheers
--
Michał (Saviq) Sawicz <mic...@sawicz.net>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/g4ADYoJAbE0J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
caveat - I have never used generic views, class based or otherwise, but
I was under the impression that if one wants customised forms or views,
generic is not the way to go.
This is admittedly a big problem with the class-based generic views.
As the person responsible for committing them, I can only offer my
humble apologies for their current state.
> Some might suggest I use Django Forms - it is a reasonable suggestion of
> course. But, being new, I don't have any "view legacy" and I just like the
> idea of working with class-based generic views where possible.
Some might, and they would be correct :-)
This isn't a legacy issue at all -- Forms and Views solve different problems.
The View is solving the problem of "how do I handle this request and
convert it into a response?". The Form is solving the problem of "How
do I convert the POST data in this request into a model object (or a
change to a model object)?".
Very roughly, a view is doing the following:
1. View gets a request
2. View works out whether this is a GET or a POST
3. If its a POST, View asks the Form to turn the Post into a model change
4. Form returns success or failure
5. View responds to the success or failure of the Form.
6. View returns a response.
The functionality of the Form is a complete subset of the
functionality of the View -- and for this reason, it's a completely
interchangable internal component.
Now, in simple situations, it's possible for a View to guess all the
defaults for the form -- all it needs to know is that you're dealing
with a Foo model, and it can construct a default Foo ModelForm.
However, if you have more sophisticated form requirements, you're
going to need a customized Form.
We *could* have implemented this by exposing all the options of
ModelForm on the View class; but in order to keep everything clean, we
kept the ModelForm isolated, and provided the View with a way to
specify which Form class it's going to use.
So - to cover your use case of excluding fields, you define a
ModelForm that excludes the fields, then let the CreateView know the
form you want to use:
class ModelForm(forms.ModelForm):
class Meta:
model = Campaign
exclude = ('user', 'name', 'content_inlined')
class CreateCampaignView(CreateView):
form_class = CampaignForm
template_name = "forms/create.html"
I'm guessing when you say "fix a values for a field", you mean setting
the values of user, name and content_inlined before you save the new
Campaign instance; to do this, you need to inject some extra code into
the form processing logic of the form:
class CreateCampaignView(CreateView):
form_class = CampaignForm
template_name = "forms/create.html
def form_valid(self, form):
self.object.user = ... (something meaningful.. e.g., self.request.user)
return super(CreateCampaignView, self).form_valid(form)
This overrides the default behavior when the form is valid, and sets
the extra values. The super() implementation of form_valid() will then
save the instance.
For the record, this could also be done by overriding the save()
method on the ModelForm -- however, if you do that, you lose the
request object, which you will need if you're trying to set the
instance values to something that is request-sensitive.
I hope I've provided some helpful clarification here.
Yours,
Russ Magee %-)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
2012/1/12 Juergen Schackmann <juergen.s...@googlemail.com>:
> can really no one help? i am really stuck here at the moment
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/RYLQqxJE7HYJ.
>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
--
Andrés Reyes Monge
arm...@gmail.com
+(505)-8873-7217
--I agree that it looks like self.object is None, so you can't set self.object.user as Russ wrote.
-- Alasdair Nicol Developer, MEMSET mail: alas...@memset.com web: http://www.memset.com/ Memset Ltd., registration number 4504980. 25 Frederick Sanger Road, Guildford, Surrey, GU2 7YD, UK.