Help with Django Models

22 views
Skip to first unread message

Udit Vashisht

unread,
Dec 13, 2017, 5:31:51 PM12/13/17
to Django users
I have an outside python function which reads a csv from web and fetch certain data in forms of tuple. So that i can use that tuple in my python models for choices in one of the fields. Till now i am running the function independently and copying the returned tuple to my models.py for choices. But there must be some better way. like pickling it etc etc. Can anyone help me with that? 

Mike Dewhirst

unread,
Dec 13, 2017, 7:14:08 PM12/13/17
to django users
I suggest you import the csv data into a table using a manage.py custom
command. Then you need to point your field requiring choices at that table.

You could perhaps then run your manage.py command on a cron schedule?


> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/723351d1-aaae-4784-8c78-ae114e5a64c2%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/723351d1-aaae-4784-8c78-ae114e5a64c2%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
Dec 13, 2017, 8:02:35 PM12/13/17
to Django users
On 14/12/2017 11:14 AM, Udit Vashisht wrote:
> Thanks for the reply. I am new to python and django. Will have to
> google a lot to understand your solution :-)

Look at models.ForeignKey ...

https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

When you add a foreign key field to a table, the Django Admin form
offers a choice of all the values from the table you specify.

If you are writing your own forms this might help ...

https://stackoverflow.com/questions/5104277/field-choices-as-queryset

A custom command just needs to follow a specific pattern and contain
functions with specific names so the command can be executed like python
manage.py fetch_web_values

https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/

Cheers

Mike


>
> On Dec 13, 2017 19:12, "Mike Dewhirst" <mi...@dewhirst.com.au
> <mailto:mi...@dewhirst.com.au>> wrote:
>
> On 14/12/2017 3:15 AM, Udit Vashisht wrote:
>
> I suggest you import the csv data into a table using a manage.py
> custom command. Then you need to point your field requiring
> choices at that table.
>
> You could perhaps then run your manage.py command on a cron schedule?
>
>
> --
> You received this message because you are subscribed to the
> Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>
> <mailto:django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>>.
> To post to this group, send email to
> django...@googlegroups.com
> <mailto:django...@googlegroups.com>
> <mailto:django...@googlegroups.com
> <https://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/723351d1-aaae-4784-8c78-ae114e5a64c2%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/optout>.
>
>

Reply all
Reply to author
Forward
0 new messages