On Sunday, April 7, 2013 11:42:50 AM UTC+5:30, Mike Dewhirst wrote:
>
> On 7/04/2013 3:27pm, surya wrote: 
> > Consider a university college. 
> > 1. College has name, uid, website 
> > 2. Each college has a many departments - Each department has name, uid. 
> > 3. Each batch in a department has name, and ratings. 
> > 
> > *from django.db import models* 
> > * 
> > * 
> > *class College(models.Model):* 
> > *name = models.CharField(max_length=200)* 
> > *uid = models.CharField(max_length=10, primary_key=True)* 
> > *website = models.URLField()* 
> > * 
> > * 
> > *def __unicode__(self):* 
> > *return self.name* 
> > * 
> > * 
> > *class Department(models.Model):* 
> > *name = models.CharField(max_length=200)* 
> > *uid = models.CharField(max_length=10)* 
> > *college = models.ForeignKey(College)* 
> > * 
> > * 
> > *def __unicode__(self):* 
> > *return self.name* 
> > * 
> > * 
> > *class Batch(models.Model):* 
> > *name = models.IntegerField(max_length=100)* 
> > *department = models.ForeignKey(Department)* 
>
> > *college = models.ForeignKey(College)* 
>
> college here is redundant because it is part of department 
>
> > *rating = models.IntegerField(default=0)* 
> > * 
> > * 
> > *def __unicode__(self):* 
> > *return self.name* 
>
> def get_college(self): 
>      return self.department.college 
>
> Also, I would omit uid in all models. Django gives you one at no charge. 
> It defaults to college.id etc which is an incrementing integer supplied 
> from the database itself. This simplifies your tasks. 
>

actually, you are right, django models do provide id.. but this uid field 
is something else.. Eg: massachusetts institute of technology. uid = MIT..i 
will be using it as primary key.

>
> See 
>
> https://docs.djangoproject.com/en/1.5/topics/db/models/#automatic-primary-key-fields
>  
>
> You would only use a custom primary key (eg your 'uid') in special 
> circumstances. Perhaps you have a legacy database with existing primary 
> keys or there is an expert insisting on it. 
>
> hth 
>
> Mike 
>
> > * 
> > * 
> > Would you suggest any better design ? (actually, I never formally read 
> DB) 
> > 
> > -- 
> > 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...@googlegroups.com <javascript:>. 
> > To post to this group, send email to 
> > django...@googlegroups.com<javascript:>. 
>
> > Visit this group at http://groups.google.com/group/django-users?hl=en. 
> > For more options, visit https://groups.google.com/groups/opt_out. 
> > 
> > 
>
>
Actually, in each College, there may be many departments, and each 
department may be in many colleges...
Can't I use ManytoManyField here? 

 

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to