Re: Dynamicly preset forms in the admin interface
2009/12/1 rebus_: > I hope this casts some light on what i am trying to say. > Anyway, the best way is to read docs and look in django code. Sorry i > can't help more. You helped a lot. Thank you. Greets, -- Kai Timmer | http://kaitimmer.de Email : em...@kaitimmer.de Jabber (Google Talk): k...@kait.de -- 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.
Re: Dynamicly preset forms in the admin interface
models.py = from django.db import models class Country(models.Model): name = CharField(max_length=70) def __unicode__(self): return '%s' % (self.name) admin.py = from django.contrib import admin from your_app.models import Country site.register(Country) When you runserver and visit add view for this model you should get simple *empty* form to add new country in your database. http://127.0.0.1:8000/admin/invest_core/country/add/ Ok, now we want to preset the name of the country. To test this you can go to this URL: http://127.0.0.1:8000/admin/invest_core/country/add/?name=Croatia The field name should now contain "Croatia". I think this explains all my muttering about GET and model attributes. = Now we want to do this but through admin not through URL. For this we change admin.py like this: admin.py = from django.contrib import admin from your_app.models import Country class CountryAdmin(admin.ModelAdmin): def add_view(self, request, form_url='', extra_context=None): # Override built in add_view request.GET = request.GET.copy() # GET is immutable QueryDict so we need to get a copy() as explaind in docs request.GET.update({'name':'Croatia'}) # This sets value for field "name" return super(CountryAdmin, self).add_view(request, form_url=form_url, extra_context=extra_context) #This calls original add_view method site.register(Country, CountryAdmin) Now when you open up add_view again the name should also be set to "Croatia" http://127.0.0.1:8000/admin/invest_core/country/add/ Background: Class ModelAdmin is located in django/contrib/admin/options.py and has add_view method which gets called when you open add view in your Django admin. URL pattern for this view is located in get_url method on the same class. This means when you subclass admin.ModelAdmin to create admin pages for you model you can override all the methods that ModelAdmin has by default or add your own methods (for custom views or whatever). So to change behaviour of add_view method you simply override it in your CountryAdmin. It still needs to return same thing (HttpResponse with some Context) as the built in add_view. I hope this casts some light on what i am trying to say. Anyway, the best way is to read docs and look in django code. Sorry i can't help more. 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.
Re: Dynamicly preset forms in the admin interface
2009/11/30 Kai Timmer: > 2009/11/29 rebus_ : >> 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.
Re: Dynamicly preset forms in the admin interface
2009/11/29 rebus_: > 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 ;) > 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 ;) Greets, -- Kai Timmer | http://kaitimmer.de Email : em...@kaitimmer.de Jabber (Google Talk): k...@kait.de -- 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.
Re: Dynamicly preset forms in the admin interface
2009/11/29 Kai Timmer: > 2009/11/29 rebus_ : >> 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.
Re: Dynamicly preset forms in the admin interface
I do not know how to do this in admin, but in your own form do this peter = article( author = "Peter" ) myform = TheFormClass ( instance = peter ) hope that helps, --Vardhan On Sun, Nov 29, 2009 at 8:41 PM, Kai Timmerwrote: > 2009/11/29 rebus_ : > > 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 > > -- > > 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. > > > -- 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.
Re: Dynamicly preset forms in the admin interface
2009/11/29 rebus_: > 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 -- 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.
Re: Dynamicly preset forms in the admin interface
2009/11/29 Kai Timmer: > Hello, > I'm new to django, so this question may look a bit stupid, but I > couldn't find the information in the documentation. > > I have a pretty simple model which looks like this: > class article(models.Model): > headline = models.CharField(max_length=140) > newsentry = models.TextField() > pub_date = models.DateTimeField('date published', null=True, > blank=True) > write_date = models.DateTimeField('started writing') > published = models.BooleanField() > author = models.CharField(max_length=20) > > What i want to do now, is that some of these fields are preset when a > new article is written. Lets say a user named Peter is going to write > a new article, then the author field should be preset to "Peter" and > shouldn't be editable. Same thing for write_date, which should be set > to the time Peter started to write this article. How do I do this? > > Greets, > Kai > > -- 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]. [1] http://docs.djangoproject.com/en/dev/intro/tutorial02/#customize-the-admin-form [2] http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model 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.
Dynamicly preset forms in the admin interface
Hello, I'm new to django, so this question may look a bit stupid, but I couldn't find the information in the documentation. I have a pretty simple model which looks like this: class article(models.Model): headline = models.CharField(max_length=140) newsentry = models.TextField() pub_date = models.DateTimeField('date published', null=True, blank=True) write_date = models.DateTimeField('started writing') published = models.BooleanField() author = models.CharField(max_length=20) What i want to do now, is that some of these fields are preset when a new article is written. Lets say a user named Peter is going to write a new article, then the author field should be preset to "Peter" and shouldn't be editable. Same thing for write_date, which should be set to the time Peter started to write this article. How do I do this? Greets, Kai -- 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.