2009/11/29 Kai Timmer <em...@kait.de>:
> 2009/11/29 rebus_ <r.dav...@gmail.com>:
>> Have you tried using fieldsets [1] in you ModelAdmin. Also you can
>> override save_model method on the ModelAdmin to set values for some
>> object attributes when the object is being saved [2].
>
> I don't see how I can use fieldsets to achieve this. I thought with
> the fieldsets I just tell django what forms to show in the interface,
> but not what to put in it.
> And when I overwrite the save_model method, there is no way to show
> the user what will end up in the database, right? So that is not the
> right way when I want to have the user to have the opportunity, to
> change the default data.
>
> Greets,
> --
> Kai Timmer | http://kaitimmer.de
> Email : em...@kaitimmer.de
> Jabber (Google Talk): k...@kait.de
>

Yes, fieldsets are used to decide what form fields to display and how
to lay them out on admin add/change views.
And yes, user would have no knowledge of what data is eventually
written in database at the time of object saving.

I must confess i haven't look into this up until now.

Seems that the add_view checks data passed through GET [1] (which is
of type QueryDict [2]) to see if any of the keys in GET correspond to
the attribute on Model, and if it finds any sets the value of that key
as initial value for the field in the admin form.

You can even set M2M fields this way by giving list of coma separated
PK's through GET for you M2M attribute.

So in your case, you can pass var though GET like so:

 /admin/app/article/add/?author=ante

and once it renders your author filed should contain word "ante".

But since you probably do not want to set your defaults directly
through URL you can wrap add_view in your ModelAdmin like this:

class ArticleAdmin(admin.ModelAdmin):
   def add_view(self, request, form_url='', extra_context=None):
      # GET is immutable QueryDict so we need to get a copy() as
explaind in docs
      request.GET = request.GET.copy()
      request.GET.update({'author':'ante'})
      return super(ArticleAdmin, self).add_view(request,
form_url=form_url, extra_context=extra_context)

When you refresh your article add view author field should contain
value of "ante".

Or if you want a full name of current user you could say:
request.GET.update({'author':request.user.get_full_name()})

I  imagine some of core devs or django gurus would maybe have better
ideas on how to do this (or can even tell you if this is documented
somewhere).

As far as readonly fields go i found snippet [3] but i haven't look at
it closely to tell you is it any good or not.
But if you set your fields read only how would you make them editable
if necessary? With JavaScript?

[1] 
http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.GET
[2] 
http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.QueryDict
[3] http://www.djangosnippets.org/snippets/937/

Hope i helped this time :)

Davor

--

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.


Reply via email to