2

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. The TemporaryJob is created when user fills the form. Next thing is to confirm. If he confirms TemporaryJob, the object should be converted to regular Job object.

class Job(models.Model):
    attributes
    methods

class TemporaryJob(Job):
    pass

I've tried Job.objects.create(temporary_job_instance) but it does not work.

5
  • 1
    Why not just using a confirmed attribute? Commented Jun 7, 2016 at 17:29
  • Because there is a signal that Job has been created which sends email to admin. So in this case admin would gets email after creation non-confirmed job. Commented Jun 7, 2016 at 17:31
  • 1
    Well, that is a matter of signal handling. Commented Jun 7, 2016 at 17:32
  • Were you able to achieve this using signals or whatever? Commented Aug 28, 2019 at 7:57
  • @GaneshJadhav I don't remember but I think @ Serafeims answer is worth to read. Commented Sep 2, 2019 at 10:14

2 Answers 2

8

This is what I do. Could be bad if you save your temporary_job_instance, so just don't.

a_dict = temporary_job_instance.__dict__
del a_dict['_state']
del a_dict['id']
Job.objects.create(**a_dict)
Sign up to request clarification or add additional context in comments.

Comments

5

First of all, Klaus D. comment is correct: I also don't think that your design is correct. 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. 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 (i.e 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).

Now you just need to create the values dict from your 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.