Hello,

I have a question for all you django wizards. I want to use different forms
for managing a model in Django's admin. I thought it's common problem, but I
cannot find a good solution even after reading through google, stackoverflow
and django's source code. (My apologies if this is a often asked newbie
question).

For example, I have a model like this:

# users/models.py
...
REG_STATUS_TYPE = (
        ( 'queued', 'Queued' ),
        ( 'sent', 'Sent' ),
        ( 'accepted', 'Accepted' ),
        ( 'declined', 'Declined' ),
        ( 'blocked', 'Blocked' ),
    )
class RegistrationQueue(models.Model):
  user = models.ForeignKey(User)
  created = models.DateTimeField(default=datetime.datetime.now,
editable=False)
  status = models.CharField(max_length=10, choices=REG_STATUS_TYPE,
default='queued')
  accepted = models.DateTimeField(editable=False, null=True)
...

# users/admin.py
...
class RegistrationViewForm(models.ModelForm):
  class Meta:
    model = RegistrationQueue

class RegistrationAddForm(models.ModelForm):
  class Meta:
    model = User

class RegistrationQueueAdmin(admin.ModelAdmin):
    add_form = RegistrationAddForm
    view_form = RegistrationViewForm

    def get_form(self, request, obj=None, **kwargs):
        defaults = {}
        if obj is None:
            defaults.update({
                'form': self.add_form,
            })
        else:
            defaults.update({
                'form': self.view_form
            })
        defaults.update(kwargs)
        return super(RegistrationAdmin, self).get_form(request, obj,
**defaults)
admin.site.register(RegistrationQueue, RegistrationQueueAdmin)

What I'm trying to do is simple really. When creating a new
RegistrationQueue entry, I would like Django's admin to use a form derived
from the User model (which would let me create a temporary user, and then I
can take some of the information from that and other things to automatically
create a RegistrationQueue entry).

However, when listing or changing the RegistrationQueue, I would like to use
a form derived from the RegistrationQueue model.

So, as you can see above, I tried to replace ModelAdmin's get_form method
with a custom one that just checks the incoming obj variable. If it's set to
None, I know it's trying a create a new entry. Otherwise, it's something
else.

The problem is that the admin always uses the form derived from the
RegistrationQueue, and never the User. Whenever I click to add a
RegistrationQueue, I get a form with fields belonging to the
RegistrationQueue model.

Please tell me what I'm doing wrong. Thank you very much in advance.

Eiji

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
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.

Reply via email to