Hi -
I'm building an application in Django, using svn to have the
post-magic-removal branch, and I'm using the admin to provide some
data setup functionality. I'm having a little trouble with validation
of a couple of models in the admin:
1 - I have the following definition of a component:
class Component(models.Model):
name = models.CharField(maxlength=50, blank=False)
project = models.ForeignKey(Project, related_name='components')
def __str__(self):
return self.name
class Meta:
ordering = ('name',)
unique_together = (('project', 'name'),)
class Admin:
pass
With the unique_together setting, I want the admin to reject the
creation or updating of a component with a name that clashes with
another component in the same project. Django's creating the unique
constraint on the table correctly, but the admin isn't validating the
uniqueness before trying to save - I get an IntegrityError from the
database. Is there a way that I can extend the admin to check this
when the item is saved so the user sees a nice error message?
2 - I've got a user record within my application's model with a
OneToOneField link to a user in the auth model. When a myapp user is
saved, I'd like to check that their settings are appropriate to the
group the corresponding auth user belongs to.
def validate_supplier(field_data, all_data):
auth_user = authmodels.User.objects.get(id=int(all_data['auth_user_id']))
if (auth_user.groups.filter(models.Q(name='DeliveryManager')
|
models.Q(name='BuildCoordinator')).count() != 0
and field_data != ''):
raise validators.ValidationError("Build Coordinators and
Delivery Managers can't have a supplier.")
if (auth_user.groups.filter(name='SupplierBuildManager').count() != 0
and field_data == ''):
raise_validators.ValidationError('Supplier Build Managers must
have a supplier.')
class User(models.Model):
auth_user = models.OneToOneField(authmodels.User, related_name='myapp_user')
supplier = models.ForeignKey(Supplier, null=True, blank=True,
related_name='users'
# All goes bad if I uncomment the validator_list
# , validator_list=[validate_supplier]
)
projects = models.ManyToManyField(Project, related_name='users')
def __str__(self):
return '%s %s (%s)' % (self.auth_user.first_name,
self.auth_user.last_name,
self.auth_user.username)
class Admin:
pass
But when I get into the validator, the all_data dict only contains
values for supplier and projects - there's no way for me to find out
what auth user has been selected.
I've tried changing the link to the auth user to be ForeignKey, which
helps. Then the auth user id is available. The other problem I see
then is that the validator isn't called when a supplier is not
selected - so there's no way I can ensure that a SupplierBuildManager
has a supplier set.
I hope these aren't things that have been asked before - I couldn't
see anything specific.
A way to solve both of these, it seems, would be to have a hook where
whole-object validation could happen before saving - is there anything
like this in the framework? I'm thinking of a .validate method that
could be added to a model class which would be called after
field-level validation, and could raise validation exceptions in the
same way.
Any ideas of how to solve these?
Thanks,
xtian
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---