On 17/09/2013 2:06pm, Lachlan Musicman wrote:
Hola,

I'm pretty sure I get this, but I thought I'd ask.

With an inventory model like:

class Movement(models.Model):
    item = models.ForeignKey(Carrier)
     date = models.DateField(default=timezone.now())
     user = models.ForeignKey(User)
     qty = models.IntegerField()

There are three things that I need to validate, or update - that the
qty isn't 0, that it isn't greater than item.qty, and to automatically
set the User as the request.user.

According to these two pages:

https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.clean

https://docs.djangoproject.com/en/1.5/topics/class-based-views/generic-editing/#models-and-request-user

These two checks are done in two different places.

As it stands, I am checking the qty in the Movement model's clean
function that I'm overriding), and the user is set in a form_valid()
on the CreateView.

I can see why these two things might be seperated, but it seems
un-DRY, Well, messy more than unDRY.

But (IMHO) it is explicit. In one case you might raise an exception and the other case involves setting a value prior to saving but after everything is validated. You can call any number of methods in Movement.clean() in precisely the sequence which pleases you.

I think both should be done in the model. Updating the user field should be done in the model so you don't have to worry about it in however many different forms you might have now or decide to have in future. It keeps the forms less messy and more about presentation than business logic.

My 2c

Mike


Am I following the docs too literally? Should I bang all of these into
a single validation. And if so, which one?

cheers
L.


--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to