0

I am new to Django. I am planning to a have a demo user account and i want to load data based on demo user's UUID(user's id). Is there another way to load the seed file without using fixtures?

seed file.

User.objects.create(
    first_name = "John",
    last_name = "Doe",
    email = "john.doef@example.com",
    gender = "M"
)

u = User.objects.last()
u.set_password("safestpassword")
u.save()


Track.objects.create(
    title = 'track1',
    description = "some description",
    image_url = "image_url",
    track_url = "track_url",
    artist = "artist",
    user_id = User.objects.last()
)
Track.objects.create(
    title = 'track2',
    description = "some description",
    image_url = "image_url",
    track_url = "track_url",
    artist = "artist",
    user_id = User.objects.last()
)

Thank you.

user7508299
  • 31
  • 1
  • 4
  • You could write your own [management command](https://docs.djangoproject.com/en/1.10/howto/custom-management-commands/) to dynamically generate your test data. But for that what you posted here, fixtures would be fine. Can you be a little more detailed about "load all the files based on demo user's UUID"? Which files do you mean and where does the UUID come from? – trixn Feb 04 '17 at 16:29
  • I have edited my question. "load all the files based on demo user's UUID". over here i wanted to the load the data file. UUID is user's id which get generated when a user is created. Can you give me an example of how to write a management command. – user7508299 Feb 04 '17 at 17:42
  • Okay but that doesn't make it a lot clearer. I doubt you will need such a "seed file" at all. If you just want a demo user account or other test data you can just create them once and maybe dump them into a json file. why do you believe that you need a script for this? – trixn Feb 05 '17 at 02:38
  • how can i dump that data into Json file? i wanted to do this so that if i drop my table and create it again i can use the seed file to reload data into it. – user7508299 Feb 05 '17 at 15:12
  • There is a management command called [dumpdata](https://docs.djangoproject.com/en/1.10/ref/django-admin/#dumpdata) for this. You can use it like this: `python manage.py dumpdata` and then you can also specify the models you want to dump explicitly. To load the data again from the fixture file use [loaddata](https://docs.djangoproject.com/en/1.10/ref/django-admin/#loaddata). See the my links for more information. – trixn Feb 05 '17 at 15:16
  • http://stackoverflow.com/documentation/django/4933/database-setup/17482/fixtures – Alex Mar 24 '17 at 12:57

0 Answers0