Hello all!

AT FIRST, THE SITUATION:

The meta attribute unique_together has no validators to check, whether
two foreign keys are unique together or not.  Instead of that, the
IntegrityError is raised [URL #1]. So I decided to code my own custom
validator for checking uniqueness of two fields.

I wrote something like this:
def isItUniqueTogether(field_data, all_data):
    from django.core import validators
    rel = None
    try:
        rel = Relation.objects.get(some_obj=int(all_data.get("some_obj")),
some_other_object=int(all_data.get("some_other_object")))
    except:
        pass
    if rel:
        raise validators.ValidationError(_("%s has been already
related to %s.") % (rel.some_object, rel.some_other_object))

and I add the validator to my model..

class Relation(models.Model):
    some_object = models.ForeignKey(SomeModel,
validator_list=[isItUniqueTogether], ...)
    some_other_object = models.ForeignKey(SomeModel,
related_name="some_name", ...)
    ...

The validator checks whether a relation already exists and in that
case it raises an error.
The problem is that if the object has just been edited (is not new),
then the edited object is assigned to rel, and the validator raises an
error not letting to resave existing relations.


AND NOW THE QUESTIONS:

Is it possible to pass a model instance or at least the id of the
object to a custom validator that is used in the model?
Or is it possible to determine in other way, whether the object is
newly created or edited.

(By the way, I am using per object permission branch)

Regards,
Aidas Bendoraitis [aka Archatas]

[URL #1] 
http://groups.google.de/group/django-users/browse_frm/thread/6a3686f48d2c70fa

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to