I have a Person
model, which has a ForeignKey
field to itself, called mother
.
When the user goes to the 'add' admin form, I want to define an initial value for mother
, in case there is a GET('mother')
parameter, or leave it blank, in case there is not.
I have actually 2 questions:
- How to access request inside
ModelAdmin
? - How to define initial value for a
ForeignKey
field?
In models.py:
class Person(models.Model):
name=models.CharField()
mother=models.ForeignKey('self')
In admin.py:
class PersonAdminForm(forms.ModelForm):
class Meta:
model = Person
class PersonAdmin(admin.ModelAdmin):
mother = request.GET.get('mother','') #don`t know how to access request
if mother != '':
form = PersonAdminForm
form.initial={'mother':Person.objects.get(id=mother)}
Well, this ain't working. Even if I only try to define a hardcoded initial value, it doesn`t work.
What am I doing wrong?
PS.: Of course, I may be asking the wrong questions, so I appreciate any help that solves the problem.