import json

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

# this relation not same as other relation table, got abit custom, so not put inside the relations.py
class TtTagRelation(models.Model):
    tag = models.ForeignKey("api.TtTag", on_delete=models.CASCADE)

    # Generic foreign key fields
    refer_table = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    refer_id = models.PositiveIntegerField()
    created_at = models.DateTimeField(auto_now_add=True)

    """
    this refer_object not a column, it is django function, when i insert, i can direct put the staff object into refer_object, eg.
    staff=TtStaff.object.create()
    TtTagRelation.object.create(tag_id=1,refer_object=staff)
    """
    refer_object = GenericForeignKey("refer_table", "refer_id")
    class Meta:
        db_table = "tt_tag_relation"
        unique_together = ("tag", "refer_table", "refer_id")