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 -- |
- Acessing data on Model/Detail classes Fabiano Martins
- Re: Acessing data on Model/Detail classes Melvyn Sopacua
- Re: Acessing data on Model/Detail classes Fabiano Martins
- Re: Acessing data on Model/Detail classes Melvyn Sopacua