Django sharing one model between two others - python

I built a system to review wines and foods. I quickly found myself repeating models and templates with tiny differences.
Fundamentally it seems I want a review to relate to either a food or a wine. And each food or wine can have many reviews.
I had an FK (current way) to both and just left one blank but given they're so similar I decided that wasn't wise.
I then went to abstract models to at least generify the fields (new way) but as I couldn't link to the generic item model I had a slightly more elegant code base for the same problem.
Researching into this I'm wondering if a generic relation from the food and wine to the review is the way to go or maybe content types but I don't get quite how they work or if they are what I'm looking for.
Current way - Wines have brands, Foods have stores and Reviews have Foods and wines
class Brand(models.Model):
brand_name = models.CharField(max_length=30)
location = models.ForeignKey(Location, null=True,blank=True)
import datetime
YEAR_CHOICES = []
for r in range(2005, (datetime.datetime.now().year+1)):
YEAR_CHOICES.append((r,r))
YEAR_CHOICES = list(reversed(YEAR_CHOICES))
class Wine(models.Model):
wine_name = models.CharField(max_length=30)
wine_type = models.ForeignKey(WineType)
wine_year = models.IntegerField( choices=YEAR_CHOICES, default=datetime.datetime.now().year)
brand = models.ForeignKey(Brand)
class Store(models.Model):
store_name = models.CharField(max_length=30)
def __str__(self):
return self.store_name
class Food(models.Model):
food_name = models.CharField(max_length=30)
food_desc = models.CharField(blank=True,max_length=100)
store = models.ForeignKey(Store)
def __str__(self):
return self.store.store_name +' - '+self.food_name
class Review(models.Model):
rating = models.CharField(max_length=30)
value = models.CharField(max_length=30)
date = models.DateField(auto_now_add=True)
person = models.ForeignKey(Person)
comment = models.CharField(blank=True,max_length=100)
food = models.ForeignKey(Food, blank=True,default=None,null=True)
wine = models.ForeignKey(Wine, blank=True,default=None,null=True)
class Meta():
ordering = ['-date']
New Way - Wines and Foods are Items, Stores and Brands are Sources, but reviews still need both Wines and foods
class Source(models.Model):
name = models.CharField(max_length=30)
desc = models.CharField(blank=True,max_length=100)
class Meta:
abstract = True
class Item(models.Model):
name = models.CharField(max_length=30)
desc = models.CharField(blank=True,max_length=100)
class Meta:
abstract = True
class WineSource(Source):
location = models.ForeignKey(Location, null=True,blank=True)
class Meta():
ordering = ['location', 'name']
class FoodSource(Source):
def __str__(self):
return self.name
import datetime
YEAR_CHOICES = []
for r in range(2005, (datetime.datetime.now().year+1)):
YEAR_CHOICES.append((r,r))
YEAR_CHOICES = list(reversed(YEAR_CHOICES))
class Wine(Item):
wine_type = models.ForeignKey(WineType)
wine_year = models.IntegerField( choices=YEAR_CHOICES, default=datetime.datetime.now().year)
source = models.ForeignKey(WineSource)
def __str__(self):
return self.source.name +' '+self.name+ ' ' + str(self.wine_type)+ ' '+ str(self.wine_year)
class Food(Item):
source = models.ForeignKey(FoodSource)
def __str__(self):
return self.source.name +' - '+self.name
class Review(models.Model):
rating = models.CharField(max_length=30)
value = models.CharField(max_length=30)
date = models.DateField(auto_now_add=True)
person = models.ForeignKey(Person)
food = models.ForeignKey(Food, blank=True,default=None,null=True)
wine = models.ForeignKey(Wine, blank=True,default=None,null=True)
#Doesn't work as it's abstract- item = models.ForeignKey(Item,null=True)
class Meta():
ordering = ['-date']

I think Generic Foreign Key is the answer. Something like:
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class Product(models.Model):
...
class Food(Product):
...
class Wine(Product):
...
class Review(models.Model):
...
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
This allows us to relate a review to any single record of any model in our project. The content_type field tracks which model you are trying to relate to (Food or Wine in this case). The object_id field track which record in the Wine or Food table we are trying to track. content_object is a convenience attribute that allows us direct access to the object (once the review has been saved).
When creating a new review you just assign the Wine or Food to the content_object field:
wine = Wine.objects.get(...)
review = Review(..., content_object=wine)
review.save()

You could also use Multi-table inheritance instead of an AbstractClass for Item. Then you can set a direct ForeignKey to Item in Review.
You can also combine this with InheritanceManager:
from model_utils.managers import InheritanceManager
class Item(models.Model):
name = models.CharField(max_length=30)
desc = models.CharField(blank=True,max_length=100)
objects = InheritanceManager()
class Wine(Item):
wine_type = models.ForeignKey(WineType)
wine_year = models.IntegerField( choices=YEAR_CHOICES, default=datetime.datetime.now().year)
source = models.ForeignKey(WineSource)
def __str__(self):
return self.source.name +' '+self.name+ ' ' + str(self.wine_type)+ ' '+ str(self.wine_year)
class Food(Item):
source = models.ForeignKey(FoodSource)
def __str__(self):
return self.source.name +' - '+self.name
class Review(models.Model):
rating = models.CharField(max_length=30)
value = models.CharField(max_length=30)
date = models.DateField(auto_now_add=True)
person = models.ForeignKey(Person)
item = models.ForeignKey(Item,null=True)
class Meta():
ordering = ['-date']
Then you can filter like this:
wine_reviews = Review.objects.exclude(item__wine__isnull=True)
food_reviews = Review.objects.exclude(item__food__isnull=True)
# and all item (directly sub-classed as wine or food:
items = Item.objects.select_subclasses().all()

Related

How can I implement the same widget that Django uses to ManyToMany fields in the admin page?

My models:
class Ingredient(models.Model):
BASE_UNIT_CHOICES = [("g", "Grams"), ("ml", "Mililiters")]
CURRENCY_CHOICES = [("USD", "US Dollars"), ("EUR", "Euro")]
ingredient_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
base_unit = models.CharField(max_length=2, choices=BASE_UNIT_CHOICES)
cost_per_base_unit = models.FloatField()
currency = models.CharField(
max_length=3, choices=CURRENCY_CHOICES, default="EUR")
def __str__(self):
return self.name
class RecipeIngredient(models.Model):
quantity = models.FloatField()
ingredient_id = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
def __str__(self):
return f"{self.quantity} / {self.ingredient_id}"
class Recipe(models.Model):
recipe_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
ingredients = models.ManyToManyField(RecipeIngredient)
date_created = models.DateTimeField('Date Created')
def __str__(self):
return f"{self.name}, {self.ingredients}"
When I use the admin page, it has this + button that allows me to create new ingredient/quantity combinations
like this
But when I try to use it from a form in my code it looks like
this
Here is my form code:
class AddRecipeForm(forms.ModelForm):
class Meta:
model = Recipe
fields = ['name', 'ingredients', 'date_created']
You should write the 'widgets' for each field in you Form that need configuration.
Check the documentation 'Widgets in forms', or even, you can define your own Widgets.

Can't I add a list to my models.py in django?

I'm quite new in Django and in this project I need to manage information about a restaurant. The problem is that I can't add more than one ingredient required to a recipe
My models.py is this:
from django.db import models
# Create your models here.
class Ingredient(models.Model):
name = models.CharField(max_length=35)
quantity = models.IntegerField()
unit = models.CharField(max_length=5)
unit_price = models.FloatField()
def __str__(self):
return self.name
class MenuItem(models.Model):
title = models.CharField(max_length=35)
price = models.FloatField()
example = models.ImageField()
def __str__(self):
return self.title
class RecipeRequirement(models.Model):
menu_items = models.ForeignKey(MenuItem, default = 1, on_delete=models.SET_DEFAULT)
ingredient = models.CharField(max_length=35)
quantity = models.FloatField()
def __str__(self):
return str(self.menu_items)
class Purchase(models.Model):
menu_item = models.ForeignKey(MenuItem, default=1, on_delete=models.SET_DEFAULT)
timestamp = models.DateTimeField()
def __str__(self):
return 'Purchase ' + str(self.menu_item) + str(self.timestamp)
After replacing ingredients = models.CharField() for ingredients = models.ManyToManyField() a list of all ingredients appear:
But when I add a new ingredient to my Ingredient model, it automatically appears in every single menu_item:
You need to have a ManyToMany relationship in your RecipeRequirement model. This will allow you to select multiple ingredients for any one RecipeRequirement object. Read more here: Django ManyToMany Fields
class RecipeRequirement(models.Model):
menu_items = models.ForeignKey(MenuItem, default = 1, on_delete=models.SET_DEFAULT)
ingredients = models.ManyToManyField(Ingredient)
quantity = models.FloatField()
def __str__(self):
return str(self.menu_items)
Set ingredients in the RecipeRequirement class as a ManyToMany field on the Ingredient class:
class RecipeRequirement(models.Model):
menu_items = models.ForeignKey(MenuItem, default = 1, on_delete=models.SET_DEFAULT)
ingredients = models.ManyToManyField(Ingredient)
quantity = models.FloatField()
def __str__(self):
return str(self.menu_items)
I believe that your Ingredientclass must have a ForeignKey associated with the RecipeRequirement in order to add more than one ingredient related to the recipe.
I suggest using a website like https://drawsql.app/ in order to obtain a better visualization of your database relationships.

Django - Am I in the right direction for creating the models of my Phone Review application?

I am a beginner in Django. I am building a data model for a Django app, named PhoneReview. It will store reviews related to the latest mobile phone. It's table should include:
a. Brand – details on brand, such as, name, origin, manufacturing since, etc
b. Model – details on model, such as, model name, launch date, platform, etc
c. Review – review article on the mobile phone and date published, etc
d. Many-to-many relationship between Review and Model.
Here are my codes in models.py:
from django.db import models
from django.template.defaultfilters import slugify
# Create your models here.
class Brand(models.Model):
brandName = models.CharField(max_length=100)
origin = models.CharField(max_length=100)
manufacturingSince = models.CharField(max_length=50, default='null')
def __str__(self):
return self.brandName
class PhoneModel(models.Model):
modelName = models.CharField(max_length=100)
launchDate = models.CharField(max_length=100)
platform = models.CharField(max_length=100)
def __str__(self):
return self.modelName
class Review(models.Model):
model_name_many_to_many = models.ManyToManyField(PhoneModel)
reviewArticle = models.CharField(max_length=1000)
datePublished = models.DateField(auto_now=True)
slug = models.SlugField(max_length=150, default='null')
def __str__(self):
return self.reviewArticle
Are my codes correct? Am I in the right direction?
Don't use camelCase in model fields. Use snake_case. Second thing is, when you want field to be default 'null', just use null=True, blank=True(optional value).
I've also provided related_name to your ManyToManyField, so you can use PhoneModelInstance.reviews.all() to get your all reviews for this specific Phone model. For large fields containing text, use TextField.
Edit
I've also added foreign key in PhoneModel which points to the Brand.
from django.db import models
from django.template.defaultfilters import slugify
# Create your models here.
class Brand(models.Model):
brand_name = models.CharField(max_length=100)
origin = models.CharField(max_length=100)
manufacturing_since = models.TextField(null=True, blank=True)
def __str__(self):
return self.brand_name
class PhoneModel(models.Model):
brand_fk = models.ForeignKey(Brand)
model_name = models.CharField(max_length=100)
launch_date = models.CharField(max_length=100)
platform = models.CharField(max_length=100)
def __str__(self):
return self.model_name
class Review(models.Model):
phone_model = models.ManyToManyField(PhoneModel, related_name='reviews')
review_article = models.TextField()
date_published = models.DateField(auto_now=True)
slug = models.SlugField(max_length=150, null=True, blank=True)
def __str__(self):
return self.review_article

Getting the entries of a ManyToManyField

I'm working with a ManyToManyField and using a ModelMultipleChoice on form, I want to get the entries, but all I get is appname.Extra.none
models.py
class Extra(models.Model):
extra_n = models.CharField(max_length=200)
extra_price = models.IntegerField(default=0)
def __str__(self):
return self.extra_n
class Meal(models.Model):
restaurant = models.ForeignKey(Restaurant, on_delete=models.PROTECT)
category = models.ForeignKey(MealCategory, on_delete=models.PROTECT)
name = models.CharField(max_length=500)
short_description = models.CharField(max_length=500)
image = models.ImageField(upload_to='meal_images/', blank=False)
price = models.IntegerField(default=0)
extras = models.ManyToManyField(Extra, related_name='extras')
def __str__(self):
return self.name
forms.py
class MealForm(forms.ModelForm):
extras = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(), queryset=Meal.extras)
class Meta:
model = Meal
exclude = ("restaurant",)
views.py
def restaurant_meal(request):
meals = Meal.objects.filter(restaurant = request.user.restaurant).order_by("-id")
return render(request, 'restaurant/meal.html', {"meals": meals})
The output desired is getting the extras added displayed on restaurant_meal view.
you can try change
meals = Meal.objects.filter(restaurant = request.user.restaurant).order_by("-id")
to
meals = Meal.objects.filter(restaurant = request.user.restaurant).prefetch_related('extras').order_by("-id")
and try again.
Doc of this in prefetch_related

relationships in django 2 models.py

I want to define a relationship between Book and Member through Borrow in models.py
ER
But I don't know how to define the Borrow relationship.
In the Borrow table it must be determined which books have been borrowed by who and which books have been returned on which date. Should I use another table for this date field?
models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext as _
class CategoryType(models.Model):
category_name = models.CharField(max_length=200)
def __str__(self):
return self.category_name
class Book(models.Model):
name = models.CharField(verbose_name="عنوان", max_length=128)
number_of_copy = models.IntegerField(default=0)
writer = models.CharField(max_length=64)
B_category = models.ForeignKey(CategoryType, on_delete=models.CASCADE)
class Meta:
ordering = ["B_category"]
def __str__(self):
return self.name
class Borrow(models.Model):
borrowed_from_date = models.DateField(_("borrow Date"), default=0)
borrowed_to_date = models.DateField(_("return Date"), default=3)
actual_return_date = models.DateField()
borrowed_by = models.ForeignKey(member, on_delete=models.CASCADE)
books = models.ManyToManyField(Book)
def __str__(self):
return self.id
class Member(AbstractUser):
pass
I think in the Member class I should have a field containing borrow_id, but how?
It seems to me that you need to use a ManyToMany relationship with a through model (this way you can store extra information for every row of the Borrow model)
...
class Borrow(models.Model):
borrowed_from_date = models.DateField(_("borrow Date"), default=0)
borrowed_to_date = models.DateField(_("return Date"), default=3)
actual_return_date = models.DateField()
borrowed_by = models.ForeignKey(Member, on_delete=models.CASCADE)
book = models.ForeignKey(Book)
def __str__(self):
return self.id
...
class Member(AbstractUser):
borrowed_books = models.ManyToManyField(Book, through='Borrow')
Maybe this link (https://docs.djangoproject.com/es/2.1/ref/models/fields/#django.db.models.ManyToManyField.through) could clarify it more.

Categories