On Thu, 2009-04-23 at 22:05 -0700, Avi wrote:
> is there a neat way to inform the admin interface that you want to
> limit hte number of one to many relationships ? (i..e. only allow 3
> addresse entires per person) ?

If "neat" means "just a simple option", then no, since that's not really
a property of many-to-one (relational algebra doesn't require a lot of
arithmetical ability. You need to grok zero, one and many... other
numbers don't exist).

However there are a couple of approaches that you might want to play
with that can help here. The admin is super-customisable, as are the
forms underlying it. Don't be afraid to use that power for your own
purposes.

Possibility #1 (the not so great approach):

The admin interface is based around forms -- you can specify a custom
ModelForm subclass for any of your models as well as a custom Formset
class. Which means you can use the full power of form validation to make
the form fail the validity test (i.e. form.is_valid() == False) if your
requirements aren't met. It will take a bit of thinking, experimenting
and proceeding in small steps, possibly to get a form that works as you
wish, but it would certainly be possible.

Realize, however, that this will still leave you with a bit of a UI
problem. It's only "kind of" a validity error to add one object too many
to the base model. In the sense that it's kind of awkward to display the
error and tell the user what they should do.

Possibility #2 (probably more promising):

Another approach is to customise the formset that is shown to the user
when they edit an admin object so that they can only add new, extra
objects when there aren't the maximum number permitted in the database.
This equates to dynamically setting the "extra" parameter on the
formset. You'd create a subclass of BaseInlineFormSet and, in it's
__init__ method do something funky to set things up properly. Reading
the existing source code for formsets and hunting out a discussion
phoebebright and I had on this list a few months back about formsets is
probably a good start for experimenting.

In both cases, experimenting and starting from a very small, simple
example is the key.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
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