Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Won Django Associations Solution #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions amazon/store/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from django.db import models


class User(models.Model):
pass


class Shop(models.Model):
pass
owner = models.OneToOneField(
'User', on_delete=models.CASCADE, related_name='shop')


class Product(models.Model):
pass
shop = models.ForeignKey(
'Shop', on_delete=models.CASCADE, related_name='products')


class Review(models.Model):
pass
product = models.ForeignKey(
'Product', on_delete=models.CASCADE, related_name='reviews')
user = models.ForeignKey(
'User', on_delete=models.CASCADE, related_name='reviews')
22 changes: 19 additions & 3 deletions grubhub/foodDelivery/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
from django.db import models


class User(models.Model):
pass


class Restaurant(models.Model):
pass


class Order(models.Model):
pass
user = models.ForeignKey(
'User', on_delete=models.CASCADE, related_name='orders')
restaurant = models.ForeignKey(
'Restaurant', on_delete=models.CASCADE, related_name='orders')


class FoodItem(models.Model):
pass
orders = models.ManyToManyField(
'Order', related_name='food_items', through='OrderFoodItem')


# orders and food_items have a many-to-many relationship
# we define order_food_items to be the intermediate table


class OrderFoodItem(models.Model):
pass
order = models.ForeignKey(
'Order', on_delete=models.CASCADE, related_name='order_food_items')
food_item = models.ForeignKey(
'FoodItem', on_delete=models.CASCADE, related_name='order_food_items')
40 changes: 40 additions & 0 deletions imdb/moviedb/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 2.1.5 on 2021-06-25 14:11

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Actor',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='Movie',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='Role',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('actor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='roles', to='moviedb.Actor')),
('movie', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='roles', to='moviedb.Movie')),
],
),
migrations.AddField(
model_name='movie',
name='actors',
field=models.ManyToManyField(related_name='movies', through='moviedb.Role', to='moviedb.Actor'),
),
]
Empty file.
12 changes: 10 additions & 2 deletions imdb/moviedb/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from django.db import models
from django.db.models.deletion import CASCADE


class Actor(models.Model):
pass


class Movie(models.Model):
pass
actors = models.ManyToManyField(
Actor, through='Role', related_name='movies')
# through specifies what the intermediate table is


class Role(models.Model):
pass
actor = models.ForeignKey(Actor, on_delete=CASCADE, related_name='roles')
movie = models.ForeignKey(Movie, on_delete=CASCADE, related_name='roles')
19 changes: 19 additions & 0 deletions medium/blog/migrations/0005_auto_20210625_1454.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.1.5 on 2021-06-25 14:54

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('blog', '0004_auto_20210625_1452'),
]

operations = [
migrations.AlterField(
model_name='comment',
name='post',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.Post'),
),
]
17 changes: 14 additions & 3 deletions medium/blog/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
from django.db import models

# Create your models here.


class User(models.Model):
pass


class Post(models.Model):
pass
# a post has one author. link a foreignkey to author
author = models.ForeignKey(
'User', on_delete=models.CASCADE, related_name='posts')


class Comment(models.Model):
pass

# a comment only belongs to one post. link a foreignkey to post
post = models.ForeignKey(
'Post', on_delete=models.CASCADE, related_name='comments')

# a comment has one author. link a foreignkey to author
author = models.ForeignKey(
'User', on_delete=models.CASCADE, related_name='comments')