简体   繁体   English

如何在Django中使用具有相同属性的另一个模型创建对象

[英]How to create an object using another model with same attributes in Django

is it possible to create an object using another object which model has the same attributes? 是否可以使用另一个具有相同属性的对象来创建对象?

In my case, I have two models - TemporaryJob and Job . 就我而言,我有两个模型TemporaryJobJob The TemporaryJob is created when user fills the form. 用户填写表单时,将创建TemporaryJob Next thing is to confirm. 接下来是要确认。 If he confirms TemporaryJob , the object should be converted to regular Job object. 如果他确认TemporaryJob ,则应将对象转换为常规Job对象。

class Job(models.Model):
    attributes
    methods

class TemporaryJob(Job):
    pass

I've tried Job.objects.create(temporary_job_instance) but it does not work. 我已经尝试过Job.objects.create(temporary_job_instance)但是它不起作用。

First of all, Klaus D. comment is correct: I also don't think that your design is correct. 首先,Klaus D.的评论是正确的:我也不认为您的设计是正确的。 Instead of having Job and TemporaryJob models with similar fields you should only have a Job model that has a boolean is_temporary field that would be True of this is a temporary job or False if not. 而不是让JobTemporaryJob模型具有相似的字段,您应该只拥有一个具有布尔is_temporary字段的Job模型,该字段的True为临时工作,否则为False If you go this way then you wouldn't need to copy the values between tables. 如果采用这种方式,则无需在表之间复制值。 All other problems you'll experience will be resolved more easily if you have normalized data. 如果您拥有标准化的数据,则将轻松地解决您将遇到的所有其他问题。

In any case, to actually answer your question, notice that objects.create() is using kwargs (ie it should be called like Job.objects.create(attr1=val1, att2=val2) etc. The best way to output these kwargs is to create a dictionary with the values of the object you want to pass (the values of temporary_job_instance ) and pass it to create using the unpacking syntax ( ** ). So, if values is a dictionary with the values of temporary_job_instance , you could just call Job.objects.create(**values) . 无论如何,要真正回答您的问题,请注意objects.create()使用的是kwargs(即,应像Job.objects.create(attr1=val1, att2=val2)这样Job.objects.create(attr1=val1, att2=val2) 。输出这些kwargs的最佳方法是创建要传递(值对象的值的字典temporary_job_instance ),并通过它来create使用拆包语法( ** )。所以,如果values是值的字典temporary_job_instance ,你可以只需调用Job.objects.create(**values)

Now you just need to create the values dict from your temporary_job_instance . 现在你只需要创建values从你的字典temporary_job_instance Unfortunately, there's no easy way to do that in django :( It can be done though -- you can take a look at theses questions for some insight and a lot of methods of doing it: Convert Django Model object to dict with all of the fields intact or this Django: Converting an entire set of a Model's objects into a single dictionary 不幸的是,在django中没有简单的方法可以做到这一点:(尽管可以做到-您可以看一下这些问题以获得一些见识和许多实现方法: 将Django Model对象转换为dict完整字段或该Django:将模型对象的整个集合转换为单个字典

This is what I do. 这就是我的工作。 Could be bad if you save your temporary_job_instance, so just don't. 如果您保存temporary_job_instance可能会很糟糕,请不要这样做。

a_dict = temporary_job_instance.__dict__
del a_dict['_state']
del a_dict['id']
Job.objects.create(**a_dict)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Django 中创建一个模型的对象,同时创建另一个不同的模型? - How to create in Django an object of a model while creating another of a different model? 模型的Django表单创建另一个模型的对象 - Django form for a model create object of another model Django-如何在同一模型中基于另一个字段创建字段 - Django-How to create a field based on another field in the same model how can we use more than one class object attributes of one model in another model in Django - how can we use more than one class object attributes of one model in another model in Django 如何在 Django 中添加另一个模型的模型对象时自动创建一个模型的模型对象 - How to create model objects of one model automatically when a model object of another has been added in django 如何在Django中为继承的模型属性创建通用的modelform字段验证器? - How to create generic modelform field validator for inherited model attributes in django? Python Django 使用同一 model 中的另一个字段填充 model 中的字段 - Python Django populate field in a model using another field in the same model Django 设置模型值,使用相同模型的另一个值 - Django set model value, using another value of the same model Django:如何将一个类的两个属性组合到同一类的另一个属性中? - Django: How to combine two attributes of a class into another attribute of the same class? 如何使用相同模型的其他字段的值在Django模型中创建一个字段? - How to create a field in django model using values of other fields of same model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM