FAQ
Hi,

I'm novice on Django, and I have a problem that I can't resolve through documentation.

I like to make a validation on Model.clear() of the master class method based on data of your
detail classes, but it returns always a empty set.

This example illustrates my problem:

1) on admin.py:
from mdtest.models import Master, Product, Service
from django.contrib import admin
from django import forms

class ProductInline(admin.TabularInline):
model = Product
extra = 0

class ServiceInline(admin.TabularInline):
model = Service
extra = 0

class MasterForm(forms.ModelForm):
class Meta:
model = Master

class MasterAdmin(admin.ModelAdmin):
form = MasterForm
inlines = [ProductInline, ServiceInline, ]

admin.site.register(Master, MasterAdmin)

2) on models.py:
from django.db import models
from django.core.exceptions import ValidationError

class Master(models.Model):
MIN_VALUE = 100.00
date = models.DateField()
description = models.CharField(max_length=200)

def clean(self):
super(Master, self).clean()
total = 0
for product in self.product_set.all():
total += product.value
for service in self.service_set.all():
total += service.value
print "Sum is " + str(total)
if total < self.MIN_VALUE :
raise ValidationError("Value can't lower than " + str(self.MIN_VALUE))

class Product(models.Model):
master = models.ForeignKey(Master)
description = models.CharField(max_length=200)
value = models.DecimalField(max_digits=12, decimal_places=2)

class Service(models.Model):
master = models.ForeignKey(Master)
description = models.CharField(max_length=200)
value = models.DecimalField(max_digits=12, decimal_places=2)

==========================

This example contains a Master class that contains two detail classes: one "product" and one
"service". The constraint of Master requires that the sum of "value" fields of both details
classes are greater than 100.00.

When I run this example, click on "add master", fill a master and some details and click "Save".
the "total" variable at clean() method always result on 0.

How do I do to access data from detail classes from methods at the master class?

Thanks in advance,
Fabiano Martins

-- Fabiano Martins Unidade de Aplicativos e Internet Divisão de Informática MPRS
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.

Search Discussions

  • Melvyn Sopacua at Jul 6, 2012 at 1:03 am

    On 5-7-2012 21:46, Fabiano Martins wrote:

    I'm novice on Django, and I have a problem that I can't resolve through
    documentation.

    I like to make a validation on Model.clear() of the master class method based on
    data of your detail classes, but it returns always a empty set.

    This example illustrates my problem:
    def clean(self):
    super(Master, self).clean()
    total = 0
    for product in self.product_set.all():
    total += product.value
    At clean() time nothing has been saved, so the relationship isn't there
    yet and you can't ask for it. There are a few ways to solve it in
    decreasing order:
    - Use a custom template and write some javascript that calculates the
    master value on submit.
    - Write a custom view and calculate the totals there.
    - Wrap everything in a transaction, do the validation in the post_save
    signal and roll back the transaction if the total is too low (not even
    sure this /can/ be done).

    Maybe others have more ideas.
    --
    Melvyn Sopacua


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
  • Fabiano Martins at Jul 6, 2012 at 3:14 pm
    My idea was centralize the validation in the Model to allow use it on
    several clients besides the primary Web page (such as a batch program, a
    web service, etc.)

    At this moment, the database relationship yet not exists, but the
    "memory data relashionship" already exists.

    Conceptually I think that, how I can access the data that will be saved
    on the Master class, I should be able to access the data that will be
    save on related classes on the same transaction.

    Now I've made a new test: I created a method "clean()" on Product class,
    and see that from it I can't access data from Master class, and I can't
    access from other instances of Product class...

    Conceptually, in the Model the data of several instances of classes on
    the same transaction can be treated as a set, or a object always be
    individually validated on Model?

    --
    Fabiano Martins
    Em 05-07-2012 22:03, Melvyn Sopacua escreveu:
    On 5-7-2012 21:46, Fabiano Martins wrote:

    I'm novice on Django, and I have a problem that I can't resolve through
    documentation.

    I like to make a validation on Model.clear() of the master class method based on
    data of your detail classes, but it returns always a empty set.

    This example illustrates my problem:

    def clean(self):
    super(Master, self).clean()
    total = 0
    for product in self.product_set.all():
    total += product.value
    At clean() time nothing has been saved, so the relationship isn't there
    yet and you can't ask for it. There are a few ways to solve it in
    decreasing order:
    - Use a custom template and write some javascript that calculates the
    master value on submit.
    - Write a custom view and calculate the totals there.
    - Wrap everything in a transaction, do the validation in the post_save
    signal and roll back the transaction if the total is too low (not even
    sure this /can/ be done).

    Maybe others have more ideas.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
  • Melvyn Sopacua at Jul 6, 2012 at 9:26 pm

    On 6-7-2012 17:14, Fabiano Martins wrote:
    My idea was centralize the validation in the Model to allow use it on
    several clients besides the primary Web page (such as a batch program, a
    web service, etc.)
    Yes, and normally the clean() method is the right method, but ...
    At this moment, the database relationship yet not exists, but the
    "memory data relashionship" already exists.
    you don't have access to the request object in the clean() method.
    Conceptually I think that, how I can access the data that will be saved
    on the Master class, I should be able to access the data that will be
    save on related classes on the same transaction.
    Yes, in the form, the view and the template. But there's no relationship
    there either. There's simply a collection of named data and because you
    know the names you can make the relationship.

    Now I've made a new test: I created a method "clean()" on Product class,
    and see that from it I can't access data from Master class, and I can't
    access from other instances of Product class...

    Conceptually, in the Model the data of several instances of classes on
    the same transaction can be treated as a set, or a object always be
    individually validated on Model?
    The relationship does not exist. Observe:

    database <=> model <=> form <=> view <=> template
    --relationship--| |-- dictionary of names --|
    So, in python there's no relationship between models. There are model
    managers that fetch the relationship from the database and return a list
    of objects and make it seem like there is a relationship, but the actual
    link is in the database. Since the products first need to know the
    primary key of the Master to make the relationship, the Master needs to
    be saved first.

    I would investigate the post_save signal.

    --
    Melvyn Sopacua


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdjango-users @
categoriesdjango, python
postedJul 5, '12 at 7:52p
activeJul 6, '12 at 9:26p
posts4
users2
websitedjangoproject.com

2 users in discussion

Fabiano Martins: 2 posts Melvyn Sopacua: 2 posts

People

Translate

site design / logo © 2022 Grokbase