from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation

# Create your models here.


class Food(models.Model):
    """
    id    name
    1     酱香饼
    2     鸡蛋饼
    3     水煎包
    """
    name = models.CharField(max_length=32)
    # 不生成字段只用于反向查询
    coupons = GenericRelation(to="Coupon")


class Fruit(models.Model):
    """
    id    name
    1     红心蜜柚
    2     黑美人西瓜
    """
    name = models.CharField(max_length=32)
    coupons = GenericRelation(to="Coupon")


class Coupon(models.Model):
    """
    id    title           food_id   fruit_id  ..........
    1     酱香饼买一送一     1        null
    2     黑美人西瓜2折      null     2

    id    title            table_id   object_id
    1     酱香饼买一送一      1            1
    2     黑美人西瓜2折       2           2

    """
    title = models.CharField(max_length=32)
    # 第一版设计
    # food = models.ForeignKey(to="Food")
    # fruit = models.ForeignKey(to="Fruit")
    # 第二版设计
    # table = models.ForeignKey(to="MyTables")
    # object_id = models.IntegerField()
    # 第三版 用django自带的ContentType表
    content_type = models.ForeignKey(to=ContentType)
    object_id = models.IntegerField()
    # 不会生成字段 只用于关联到对象的
    content_object = GenericForeignKey("content_type", "object_id")
    


# class MyTables(models.Model):
#     """
#     id     app_name    table_name
#     1       Demo        Food
#     2       Demo        Fruit
#     """
#     app_name = models.CharField(max_length=32)
#     table_name = models.CharField(max_length=32)

d05d1393d58d73a0cf9dcce4a982112d2c462450
<mask token>


class Fruit(models.Model):
    """
    id    name
    1     红心蜜柚
    2     黑美人西瓜
    """
    name = models.CharField(max_length=32)
    coupons = GenericRelation(to='Coupon')


class Coupon(models.Model):
    """
    id    title           food_id   fruit_id  ..........
    1     酱香饼买一送一     1        null
    2     黑美人西瓜2折      null     2

    id    title            table_id   object_id
    1     酱香饼买一送一      1            1
    2     黑美人西瓜2折       2           2

    """
    title = models.CharField(max_length=32)
    content_type = models.ForeignKey(to=ContentType)
    object_id = models.IntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

<mask token>


class Food(models.Model):
    <mask token>
    <mask token>
    <mask token>


class Fruit(models.Model):
    """
    id    name
    1     红心蜜柚
    2     黑美人西瓜
    """
    name = models.CharField(max_length=32)
    coupons = GenericRelation(to='Coupon')


class Coupon(models.Model):
    """
    id    title           food_id   fruit_id  ..........
    1     酱香饼买一送一     1        null
    2     黑美人西瓜2折      null     2

    id    title            table_id   object_id
    1     酱香饼买一送一      1            1
    2     黑美人西瓜2折       2           2

    """
    title = models.CharField(max_length=32)
    content_type = models.ForeignKey(to=ContentType)
    object_id = models.IntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

<mask token>


class Food(models.Model):
    <mask token>
    name = models.CharField(max_length=32)
    coupons = GenericRelation(to='Coupon')


class Fruit(models.Model):
    """
    id    name
    1     红心蜜柚
    2     黑美人西瓜
    """
    name = models.CharField(max_length=32)
    coupons = GenericRelation(to='Coupon')


class Coupon(models.Model):
    """
    id    title           food_id   fruit_id  ..........
    1     酱香饼买一送一     1        null
    2     黑美人西瓜2折      null     2

    id    title            table_id   object_id
    1     酱香饼买一送一      1            1
    2     黑美人西瓜2折       2           2

    """
    title = models.CharField(max_length=32)
    content_type = models.ForeignKey(to=ContentType)
    object_id = models.IntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation


class Food(models.Model):
    """
    id    name
    1     酱香饼
    2     鸡蛋饼
    3     水煎包
    """
    name = models.CharField(max_length=32)
    coupons = GenericRelation(to='Coupon')


class Fruit(models.Model):
    """
    id    name
    1     红心蜜柚
    2     黑美人西瓜
    """
    name = models.CharField(max_length=32)
    coupons = GenericRelation(to='Coupon')


class Coupon(models.Model):
    """
    id    title           food_id   fruit_id  ..........
    1     酱香饼买一送一     1        null
    2     黑美人西瓜2折      null     2

    id    title            table_id   object_id
    1     酱香饼买一送一      1            1
    2     黑美人西瓜2折       2           2

    """
    title = models.CharField(max_length=32)
    content_type = models.ForeignKey(to=ContentType)
    object_id = models.IntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation

# Create your models here.


class Food(models.Model):
    """
    id    name
    1     酱香饼
    2     鸡蛋饼
    3     水煎包
    """
    name = models.CharField(max_length=32)
    # 不生成字段只用于反向查询
    coupons = GenericRelation(to="Coupon")


class Fruit(models.Model):
    """
    id    name
    1     红心蜜柚
    2     黑美人西瓜
    """
    name = models.CharField(max_length=32)
    coupons = GenericRelation(to="Coupon")


class Coupon(models.Model):
    """
    id    title           food_id   fruit_id  ..........
    1     酱香饼买一送一     1        null
    2     黑美人西瓜2折      null     2

    id    title            table_id   object_id
    1     酱香饼买一送一      1            1
    2     黑美人西瓜2折       2           2

    """
    title = models.CharField(max_length=32)
    # 第一版设计
    # food = models.ForeignKey(to="Food")
    # fruit = models.ForeignKey(to="Fruit")
    # 第二版设计
    # table = models.ForeignKey(to="MyTables")
    # object_id = models.IntegerField()
    # 第三版 用django自带的ContentType表
    content_type = models.ForeignKey(to=ContentType)
    object_id = models.IntegerField()
    # 不会生成字段 只用于关联到对象的
    content_object = GenericForeignKey("content_type", "object_id")
    


# class MyTables(models.Model):
#     """
#     id     app_name    table_name
#     1       Demo        Food
#     2       Demo        Fruit
#     """
#     app_name = models.CharField(max_length=32)
#     table_name = models.CharField(max_length=32)
