简体   繁体   中英

django models table with foreign key

I want to give access to Messages with another table to the Message table, many users can access message. I am confused on how to declare the foreign keys

   class Message(models.Model):

            ID = models.CharField(_("ID"), max_length=140)
            body = models.TextField(_("Body"))
            permission=models.ForeignKey(Permission,on_delete=models.CASCADE,verbose_name=
            _("permission"),null=True,black=True)    

Second Table:

class Permission(models.Model):
    #multiple users have to access to Message 


    Users = models.ForeignKey(AUTH_USER_MODEL, related_name='access', 
    verbose_name=_("UserWithAccess"),   on_delete=models.PROTECT)  

Firs import User model:

from django.contrib.auth.models import User

Then call the User model within your Foreign key field:

Users = models.ForeignKey(User, related_name='access', 
verbose_name=_("UserWithAccess"),   on_delete=models.PROTECT)  

Hope this helps. Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM