0

Its Django 1.7, I have User model with date_joined field. I have another related model Userprofile. Also, the database is already existing with few thousand entries already.

Now, i want to add this date_joined field to userprofile

date_joined = models.DateTimeField(auto_now_add=True, null=True)

And for the already existing rows in the Userprofile model, i want to put the value already there in the User model.

Although i can write a simple function to do this, I was curious if there is a simpler way to do that through models directly during creating that column only.

M.javid
  • 6,387
  • 3
  • 41
  • 56
Rohit
  • 475
  • 1
  • 7
  • 16
  • Yes, write an sql query. It's just a one liner. – e4c5 May 04 '16 at 04:53
  • It could also be done using [data migration](http://stackoverflow.com/questions/25960850/loading-initial-data-with-django-1-7-and-data-migrations) – AKS May 04 '16 at 05:20

1 Answers1

0

As you already know, date_joined is already present in your data - Django provides it by default. It sounds like you want to duplicate that existing data into your UserProfile instances. Duplicating data is never a good idea. It can go out of sync, you need to pepper your code with synchronization functions, etc. My advice is to NOT try to do what you're trying to do. Just utilize the existing data as needed. Django is giving you a "gimme" here and it sounds like you want to make things more complicated than they need to be. Remove date_joined from UserProfile.

shacker
  • 14,712
  • 8
  • 89
  • 89