For a foreign keys you have to specify an on_delete attribute,

food=models.ForiegnKey(Food,on_delete=models.CASCADE)

If a row in the parent table(food for example) is deleted this will cause
the row/s that refers to that particular food item in your Restraunt model
be deleted.

on_delete can be given other options as SET_NULL,DO_NOTHING. Refer the docs
they are extensive on this topic.

On Nov 23, 2018 1:20 AM, "Abba Haruna" <abbamu...@gmail.com> wrote:

> from django.db import models
>
> class Food(models.Model):
> name = models.CharField(max_length=30)
> picture = models.ImageField(upload_to='images/', null=True)
> def __unicode__(self):
> return self.name
> class Town(models.Model):
> name = models.CharField(max_length=30)
>
> def __unicode__(self):
> return self.name
> class Restaurant(models.Model):
> name = models.CharField(max_length=100)
> desc = models.CharField(max_length=100)
> menu = models.CharField(max_length=100)
> web = models.CharField(max_length=100)
> phone = models.CharField(max_length=40)
> address = models.CharField(max_length=100)
> post_code = models.CharField(max_length=20)
> picture = models.ImageField(upload_to='images/', null=True)
> map = models.ImageField(upload_to='images/', null=True)
> gmap_url = models.CharField(max_length=200, null=True)
> food = models.ForeignKey(Food)
> town = models.ForeignKey(Town)
> STARS = ((1,'one'),
> (2,'two'),
> (3,'three'),
> (4,'four'),)
> votes = models.IntegerField(choices=STARS, default=4)
> def __unicode__(self):
> return self.name
> def __str__(self):
>
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/de6ad3df-e330-43e1-875a-58895e245851%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/de6ad3df-e330-43e1-875a-58895e245851%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMMZq8OuObyCbPXrwb47uSozX3sk7RXPiHaJnZxV%3Dk6SPjYpuA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to