class Game(models.Model):
Rate = (
('1 star', 1),
('2 stars', 2),
('3 stars', 3),
('4 stars', 4),
('5 stars', 5),
)
rating = models.CharField(max_length=7, choices=Rate)
title = models.CharField(max_length=100, default='Mario')
publisher = models.CharField(max_length=100, null=True)
developer = models.CharField(max_length=100, null=True)
avg = models.FloatField(default=0)
def update_avg(self, title):
self.avg = Game.objects.filter(title=title).aggregate(Avg('rating'))
def __str__(self):
return self.title + " " + str(self.rating)
def save(self, *args, **kwargs):
self.avg = self.update_avg(self.title)
super(Game, self).save(*args, **kwargs)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.SET_DEFAULT, default='user')
# games = models.ManyToManyField(Game)
def __str__(self):
return str(self.user.username)
super(Game, self).save(*args, **kwargs)
class AvgRating(models.Model):
_avg = models.FloatField(default=0)
all = models.IntegerField(default=0)
def set_avg(self, rate = 0):
self.is_updated = True
self.all += 1
self._avg = (self._avg * self.all + rate) / self.all
self.is_set = True
def get_avg(self):
return self._avg
avg = property(get_avg, set_avg)
from django.db import models
# from django.contrib.postgres.fields import ArrayField
class Cocktail(models.Model):
title = models.CharField(max_length=50, db_index=True, verbose_name='Название')
ingredients = models.ManyToManyField('Ingredient', verbose_name='Ингредиенты', through='MyBar',)
def __str__(self):
return f"{self.title}: id = {self.id} "
class Meta:
verbose_name_plural = 'Коктейли'
verbose_name = 'Коктейль'
class Ingredient(models.Model):
PORTIONS = [
('50 ml', 'одна часть'),
('75 ml', 'полторы части'),
('100 ml', 'две части'),
('150 ml', 'три части'),
]
portion = models.CharField(max_length=10, choices=PORTIONS, default=PORTIONS[0][0], verbose_name='Порция')
title = models.CharField(max_length=30, db_index=True, verbose_name='Название')
def __str__(self):
return f"{self.title}"
class Meta:
verbose_name_plural = 'Ингредиенты'
verbose_name = 'Ингредиент'
class MyBar(models.Model):
ingredients = models.ForeignKey('Ingredient', on_delete=models.CASCADE)
cocktail = models.ForeignKey('Cocktail', on_delete=models.CASCADE)
from django.db import models
# from django.contrib.postgres.fields import ArrayField
class MyBar(models.Model):
cocktail = models.ForeignKey('Cocktail', null=True, on_delete=models.SET_NULL)
ingredients = models.ForeignKey('Ingredient', null=True, on_delete=models.SET_NULL)
class Cocktail(models.Model):
title = models.CharField(max_length=50, db_index=True, verbose_name='Название')
# ingredients = ArrayField(, blank=True)
ingredients = models.ManyToManyField('Ingredient', null=True, verbose_name='Ингредиенты')
# portions = models.ForeignKey('Portion', null=True, on_delete=models.DO_NOTHING, verbose_name='порции')
# portions = models.ManyToManyField('Portion', verbose_name='Порции')
# Stars or rating = Many to One
def __str__(self):
return f"{self.title}: id = {self.id} "
class Meta:
verbose_name_plural = 'Коктейли'
verbose_name = 'Коктейль'
class Ingredient(models.Model):
PORTIONS = [
('50 ml', 'одна часть'),
('75 ml', 'полторы части'),
('100 ml', 'две части'),
('150 ml', 'три части'),
]
portion = models.CharField(max_length=10, choices=PORTIONS, default=PORTIONS[0][0], verbose_name='Порция')
# portion = models.CharField(max_length=6, choices=PORTIONS, blank=True)
title = models.CharField(max_length=30, db_index=True, verbose_name='Название')
# portion = models.ForeignKey('Portion', null=True, blank=True, on_delete=models.SET_NULL)
# portion = models.ManyToManyField('Portion', verbose_name='Порция')
def __str__(self):
return f"{self.title}"
class Meta:
verbose_name_plural = 'Ингредиенты'
verbose_name = 'Ингредиент'
# class Portion(models.Model):
#
#
# def __str__(self):
# return self.portion
#
# class Meta:
# verbose_name_plural = 'Порции'
# verbose_name = 'Порция'
from django.db import models
# from django.contrib.postgres.fields import ArrayField
class Cocktail(models.Model):
title = models.CharField(max_length=50, db_index=True, verbose_name='Название')
# ingredients = ArrayField(, blank=True)
ingredients = models.ForeignKey('Ingredient', null=True, on_delete=models.SET_NULL, verbose_name='Ингредиенты')
portions = models.ForeignKey('Portion', null=True, on_delete=models.DO_NOTHING, verbose_name='порции')
# portions = models.ManyToManyField('Portion', verbose_name='Порции')
# Stars or rating = Many to One
def __str__(self):
return f"{self.title}: id = {self.id} "
class Meta:
verbose_name_plural = 'Коктейли'
verbose_name = 'Коктейль'
class Ingredient(models.Model):
# portion = models.CharField(max_length=6, choices=PORTIONS, blank=True)
title = models.CharField(max_length=30, db_index=True, verbose_name='Название')
# portion = models.ForeignKey('Portion', null=True, blank=True, on_delete=models.SET_NULL)
portion = models.ManyToManyField('Portion', verbose_name='Порция')
def __str__(self):
return f"{self.title}"
class Meta:
verbose_name_plural = 'Ингредиенты'
verbose_name = 'Ингредиент'
class Portion(models.Model):
PORTIONS = [
('50 ml', 'одна часть'),
('75 ml', 'полторы части'),
('100 ml', 'две части'),
('150 ml', 'три части'),
]
portion = models.CharField(max_length=10, choices=PORTIONS, null=True, verbose_name='Порция')
def __str__(self):
return self.portion
class Meta:
verbose_name_plural = 'Порции'
verbose_name = 'Порция'