2009/11/30 Kai Timmer <em...@kait.de>:
> 2009/11/29 rebus_ <r.dav...@gmail.com>:
>> 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).
> What i wonder is: Isn't this a fairly common thing? I could think of
> so many situations where you would preset fields with values from the
> database. So I am pretty suprised that it seems like there is no
> "default" way of doing this. This maybe because i never used a
> framework like django before and I'm missing a general design
> principle. If so, please tell me ;)
>
Well default would be to set "default" on your models.

class SomeModel(models.Model):
   level = IntegerField(default=22)

When you open add view in admin this field would have number 22 already set.
But it would always have 22 set, and you would have to work some magic
to change it in runtime.

Usually, i don't care if people can set preset fields, so i override
save_model method and populate fields i want with the values i want.
For example if i want to set author of the article i just set it
before it gets written to database to value of
request.user.get_full_name()

request.user.get_full_name() would return the full name of the user
that is currently logged in (in other words owns a request)

Still i am not sure about read-only fields. But i guess you could use
custom templates in admin to achieve this, or override form widgets.

>> 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?
> It is more like the fields should be either preset and editable XOR
> preset and not editable. Nothing to change there once the page is
> rendered
>
>> Hope i helped this time :)
> Too much Information :) I'm just getting started with django ;)

Example i gave in previous email can be used to set different values
for different cases in different requests and wrapping add_view method
seems fairly easy.
Of course, to do this you have to be familiar with Django at least a
bit. All you need to do is create your AdminModel (which is described
in Django tutorial among other places) and inside of that class create
method add_view (for which i gave example in previous mail).

Also using GET methods to retrieve data is common in HTTP protocol and
it is not something specific to Django.

If your URL looks like http://127.0.0.1/?variable=value in PHP you
would use $_GET["variable"]; to get the value while in Django you
would use request.GET["variable"].

If any of variables in request.GET have the same name as some
attribute on your model, fields for that attribute is set to value of
request.GET["that_var"].

I can give you full code example if it would make it easier to understand.
I believe this can sound overwhelming but as people tend to say, it's
just python :)

And as i have said, there might be easier ways to do this, but i can't
think of any, and if there are i would like to see them myself too.

Also, i would suggest further reading:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/
http://docs.djangoproject.com/en/dev/topics/forms/
http://docs.djangoproject.com/en/dev/ref/request-response/

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