Re: Model design question
On 18/05/2012 12:25pm, oneroler wrote:
> Thanks Mike, that is what I was originally planning to do but realized
> there would be situations where that wouldn't do exactly what I
> wanted. For example, if there is a business that only has the
> strategy 'wholesale' assigned, using ForeignKey would still allow me
> to assign a different strategy to a division. I was hoping to find a
> solution where the strategy for a division is constrained by the
> strategies assigned to its respective business.
It is done in the Admin by nesting admin.StackedInline classes.
Essentially a queryset provides the choices for selecting a Strategy
from those belonging to the Division's parent Business. Perhaps that is
the way to do it in your forms.
To prevent incorrect Strategy assignment, I would build a
Division.clean() method which tests whether the Division.strategy is valid.
I'm a beginner at this but here is a stab at a Division.clean() and
maybe a dguru can contribute ...
Bear in mind that there will be an automatically created
business_strategy table to carry all the many-to-many relationships. I
would replace it with my own called (say) BusinessStrategy and in the
Business model use the 'through' attribute to specify that table.
class BusinessStrategy(models.Model):
business = models.ForeignKey(Business)
strategy = models.ForeignKey(Strategy)
Then I could make a Division.clean() method something like this ...
def clean(self):
for item in BusinessStrategy.objects.all(business=self.business):
if self.strategy == item.strategy:
return True
raise ValidationError(u'Invalid strategy)
>
> On Thursday, May 17, 2012 5:55:11 PM UTC-7, Mike Dewhirst wrote:
>
> On 18/05/2012 7:02am, oneroler wrote:
> > I'm trying to setup my first app and I'm trying to figure out
> the best
> > way to have constraints on a particular field (strategy for class
> > Division noted below). Below is the basic model structure. What I
> > would like is for the strategy under a Division to be
> constrained to
> > the strategies selected for the Business. A business may have many
> > strategies, but a division will only have one (but it should
> only be
> > one selected for the business). Any help on this would be
> > appreciated. Thanks, Sam
> >
> > class Strategy(models.Model):
> > name = models.CharField(max_length=200)
> >
> > #name would be something like retail, wholesale, etc
> >
> > class Business(models.Model):
> > name = models.CharField()
> > strategy = models.ManyToManyField(Strategy)
> >
> > class Division(models.Model):
> > business = models.ForeignKey(Business)
> > name = models.CharField()
> > strategy = ???
>
> Try ...
>
> strategy = models.ForeignKey(Strategy)
>
>
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Django users" group.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msg/django-users/-/sunwQb8Ft0cJ
> <https://groups.google.com/d/msg/django-users/-/sunwQb8Ft0cJ>.
> > To post to this group, send email to
> django-users@googlegroups.com <mailto:django-users@googlegroups.com>.
> > To unsubscribe from this group, send email to
> > django-users+unsubscribe@googlegroups.com
> <mailto:django-users%2Bunsubscribe@googlegroups.com>.
> > For more options, visit this group at
> > http://groups.google.com/group/django-users?hl=en
> <http://groups.google.com/group/django-users?hl=en>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/-hITt8lS1f0J.
> 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.
--
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home