简体   繁体   中英

Django Models.py Database Design Feedback

Based on my previous question and feedback I received I have redesigned my Models and need some feedback before I run the " syncdb ".

My concerns are mostly ForeignKeys and the one ManyToManyField in the Restaurant table. Should also the ManyTomany field have the through='' value and what the value should be?

Any feedback is appreciated!

Models

class Restaurant(models.Model):
    id = models.AutoField(primary_key=True, db_column='id')
    name = models.CharField(max_length=50L, db_column='name', blank=True)
    address = models.CharField(max_length=100L, blank=True)
    city_id = models.ForeignKey('City', related_name="restaurant_city")
    location_id = models.ForeignKey('Location', related_name="restaurant_location")
    hood_id = models.ForeignKey('Hood', null=True, blank=True, related_name="restaurant_hood")
    listingrole_id = models.ForeignKey('Listingrole', related_name="restaurant_listingrole")
    cuisine_types = models.ManyToManyField('Cuisinetype', null=True, blank=True, related_name="restaurant_cuisinetype")
    class Meta:
        db_table = 'restaurant'

class City(models.Model):
    id = models.AutoField(primary_key=True, db_column='id')
    name = models.CharField(max_length=50L, db_column='city')
    state = models.CharField(max_length=50L, db_column='state', blank=True, null=True)
    class Meta:
        db_table = 'city'

class Cuisinetype(models.Model):
    id = models.AutoField(primary_key=True, db_column='id')
    name = models.CharField(max_length=50L, db_column='cuisine', blank=True) # Field name made lowercase.
    class Meta:
        db_table = 'cuisinetype'

class Location(models.Model):
    id = models.AutoField(primary_key=True, db_column='id')
    name = models.CharField(max_length=50L, db_column='location', blank=False, null=False)
    city = models.ForeignKey('City', related_name="location_city")
    class Meta:
        db_table = 'location'

class Hood(models.Model):
    id = models.AutoField(primary_key=True, db_column='id')
    name = models.CharField(max_length=50L, db_column='hood')
    city = models.ForeignKey('City', related_name='hood_city')
    location = models.ForeignKey('Location', related_name='hood_location')
    class Meta:
        db_table = 'hood'    

class Listingrole(models.Model):
    id = models.AutoField(primary_key=True, db_column='id')
    name = models.CharField(max_length=50L, db_column='listingrole', blank=True) # Field name made lowercase.
    class Meta:
        db_table = 'listingrole'
....

Having into account the concept and meaning of coisine_types you don't have to make the relationship using through keyword. You use it (mostly) when there is some information about the relation it self.

According Django documentation:

The most common use for this option is when you want to associate extra data with a many-to-many relationship.

See explanation here: Extra fields on many-to-many relationships

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM