Well, I got it. In a very UGLY way, but it does its job.
I modified the inline class so that it gets access to the request object by
overriding the get_formset function
class CheckupInLineAdmin(admin.StackedInline):
model = Checkup
* form =CheckupAdminForm*
extra = 1
*def get_formset(self, request, obj=None, **kwargs):
self.form.request=request
return super(CheckupInLineAdmin, self).get_formset(request, obj,
**kwargs)*
Then I defined a form for this class overriding the __init__ function to
give proper initial values:
class CheckupAdminForm(forms.ModelForm):
class Meta:
model = Checkup
def __init__(self, *args, **kwargs):
super(CheckupAdminForm, self).__init__(*args, **kwargs)
# if it is not an instance (an already saved object)
if not kwargs.has_key('instance'):
# get patient id from request path
# UGLY but I did not find a better way...
# the request path is something like
# /admin/myapp/patient/1/
# where 1 is the patient id
splitted = self.request.META['PATH_INFO'].split('/')
# check the last bit is a int
# this is useful when creating a new patient. In this case
# the request path is
# /admin/myapp/patient/add/ and the last bit is not the
patient id
try:
id_pat = int(splitted[len(splitted)-2])
pat = Patient.objects.get(id=id_pat)
checkups = pat.checkups.all()
how_many = len(checkups)
# take the last checkup
if how_many > 0:
checkup = checkups[how_many - 1]
# initialize fields depending on the last chechup
values
self.initial['alcol'] = checkup.alcol
except:
pass
If someone knows a better way, any hint is appreciated.
Cheers
FraMazz
--
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/-/-MzJn8ScMvsJ.
To post to this group, send email to django-users@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.