我是 Django - ORM 的新手,面临创建关系的问题。 数据库是 SQLite 。
我正在尝试解决以下问题 -
用户将为他/她的个人资料添加不同的技能。他/她也可以 创造以前不存在的新技能,并可以添加到 个人资料。
其他用户可以在用户档案上提升技能 - 这应该是 也记录在数据库中。
直到我没有创建以下模型
#import from django models module
from django.db import models
# This class will more or less map to a table in the database and defines skills at the application level
class Skill (models.Model):
# this defines a required name that cannot be more than 100 characters.
skill_name = models.CharField(max_length=100,unique=True)
class Meta:
app_label = "wantedly_webapp"
# This class will more or less map to a table in the database and defines the many to many relationship between user-skill, this is our intermediate model
class UserSkill(models.Model):
""" A Model for representing skill in user profile """
unique_together = (('user', 'skill_item'),)
user = models.ForeignKey('UserProfile',on_delete=models.CASCADE)
# Define a foreign key relating this model to the Skill model.
# The parent user will be able to access it's skills with the related_name
# 'all_user_skills'. When a parent is deleted, this will be deleted as well.
skill_item = models.ForeignKey(
Skill,
related_name='all_user_skills', on_delete=models.CASCADE
)
def __str__(self):
"""Return a human readable representation of the model instance."""
return "{}".format(self.skill_item.skill_name)
# this class adds a Many to Many field in existing django-rest auth UserProfile class for user and his/her skills
class UserProfile(models.Model):
user = models.OneToOneField('auth.User',unique=True,on_delete=models.CASCADE)
user_skills = models.ManyToManyField(
Skill,
through='UserSkill',
through_fields=('user','skill_item')
)
class UserSkillUpvotes(models.Model):
unique_together = (('user_skill', 'upvote_by'),)
user_skill = models.OneToOneField('UserSkill',unique=True,on_delete=models.CASCADE)
upvote_by = models.OneToOneField('auth.User',unique=True,on_delete=models.CASCADE)
我现在已经创建了模型,接下来我需要有关如何测试它的帮助并更新是否需要执行任何操作,例如