django python 修改外键从表的非主键字段值的方法
如何修改从表Category表中的parent_project对应从表的need_create_file字段的值?
class Category(models.Model):
id = models.UUIDField(auto_created=True,default=uuid.uuid1,primary_key=True,editable=False,db_index=True,blank=False,unique=True,verbose_name='分类主键',help_text='分类主键')
name = models.CharField(max_length=200,blank=False,verbose_name='组件名',help_text='组件名')
desc = models.CharField(max_length=200,blank=True,verbose_name='组件描述',help_text='组件描述')
category_type = models.IntegerField(choices=settings.CATEGORY_TYPE,verbose_name='组件级别',help_text='组件级别')
status = models.IntegerField(default=0,verbose_name='组件状态',help_text='组件状态')
create_time = models.DateTimeField(verbose_name='创建时间',default=datetime.now,help_text='创建时间')
create_user = models.ForeignKey(User,null=True,blank=True,related_name='category_create_user',help_text='创建用户',verbose_name='创建用户',on_delete=models.SET_NULL)
attorn_time = models.DateTimeField(verbose_name='修改时间',default=datetime.now,help_text='修改时间')
attorn_user = models.ForeignKey(User,null=True,blank=True,related_name='category_attorn_user',help_text='修改用户',verbose_name='修改用户',on_delete=models.SET_NULL)
parent_category = models.ForeignKey('self',null=True,blank=True,default=None,db_index=True,help_text='所属模块',verbose_name='所属模块',related_name='child',on_delete=CASCADE)
parent_project = models.ForeignKey(ProjectAuto,null=True,blank=True,db_index=True,default=None,help_text='归属项目',verbose_name='归属项目',to_field='auto_id')
class ProjectAuto(models.Model):
"""
脚本执行
"""
auto_id = models.UUIDField(auto_created=True,default=uuid.uuid1,primary_key=True,db_index=True,blank=False,unique=True,verbose_name='项目主键',help_text='项目主键')
project_id = models.ForeignKey(Project,verbose_name='案例项目编号',help_text='案例项目编号',blank=True,null=True,related_name='auto_cate')
create_time = models.DateTimeField(verbose_name='创建时间',default=datetime.now,help_text='创建时间')
create_user = models.ForeignKey(User, null=True, blank=True, verbose_name='创建用户',help_text='创建用户',related_name='create_user',on_delete=models.SET_NULL)
attorn_user = models.ForeignKey(User, null=True, blank=True, verbose_name='授权用户',help_text='授权用户',related_name='attorn_user',on_delete=models.SET_NULL)
attorn_time = models.DateTimeField(null=True, blank=True,verbose_name='授权时间',default=None,help_text='授权时间')
project_name = models.CharField(unique=True,null=False, blank=False,max_length=200,verbose_name='项目名称',help_text='项目名称')
project_path = models.CharField(default=settings.JOB_FILES_PATH,max_length=300,verbose_name='项目存放路径',help_text='项目存放路径')
allow_other_edit = models.BooleanField(verbose_name='是否允许其它用户编辑',default=False,help_text='是否允许其它用户编辑')
need_create_file = models.BooleanField(verbose_name='是否需要重新生成实体工程',default=True,help_text='是否需要重新生成实体工程')
old_project = models.BooleanField(verbose_name='是否旧工程',default=False,help_text='是否旧工程')
version = models.DecimalField(default=0.1,blank=True,decimal_places=1,max_digits=10,help_text='组件版本号',verbose_name='组件版本号')
dept_project_name = models.CharField(null=True, blank=True,default=None,max_length=200,verbose_name='仓库项目名称',help_text='仓库项目名称')
实现方法:
cate_obj=Category.objects.filter(pk=category_id).first() category_ = category_obj.parent_category.parent_project category_.need_create_file=True category_.save()