function to create objects in a given model

31 views
Skip to first unread message

Chansons

unread,
May 20, 2014, 3:09:47 PM5/20/14
to django...@googlegroups.com
Hello everybody,

I'm new to django, and writing my first app, I have a question before deploying it.

I need to have a function that would create or modify, according to a few rules and a precise instance of a model (Song), many instances of another model (Words) at once.
Writing that function is easy, my problem is, where should I put it, so it's easy to use when the application will be in production?
Should it be in the model layer? Or in a separate module? Will I call it with the shell? Is it possible to access it via the admin site? Or any other way?

I don't find anything about that in the docs, or in google, any ideas will be welcome. I can be more precise if needed...
Thanks


Venkatraman S

unread,
May 20, 2014, 3:32:19 PM5/20/14
to django...@googlegroups.com
On Tue, May 20, 2014 at 8:39 PM, Chansons <chanson...@gmail.com> wrote:
Hello everybody,

I'm new to django, and writing my first app, I have a question before deploying it.

I need to have a function that would create or modify, according to a few rules and a precise instance of a model (Song), many instances of another model (Words) at once.
Writing that function is easy, my problem is, where should I put it, so it's easy to use when the application will be in production?
Should it be in the model layer? Or in a separate module? Will I call it with the shell? Is it possible to access it via the admin site? Or any other way?


Chansons

unread,
May 20, 2014, 6:45:22 PM5/20/14
to django...@googlegroups.com
Hello,

thanks for your answer, I knew about fixtures, but I'm not sure it's really adapted to what I need.

Adding or modifying those instances of a model will depend on a precise instance of another model: when I create a song, I also want to find in the existing glossary / or add the words of the song. I will have to do that often, each time I add a new song.
If I use fixture, I would have to modify or create the instances 'outside' the application, and use loaddata afterwards.

Isn't there an easier, more direct way to do this? Or perhaps, I didn't quite understand what you meant by that?

 



Le mardi 20 mai 2014 17:32:19 UTC+2, Venkatraman.S. a écrit :

Ilya Kazakevich

unread,
May 20, 2014, 6:50:34 PM5/20/14
to django...@googlegroups.com
Hello,

Try managers: https://docs.djangoproject.com/en/1.6/topics/db/managers/

Ilya Kazakevich,
JetBrains PyCharm (Best Python/Django IDE)
http://www.jetbrains.com/pycharm/
"Develop with pleasure!"
>--
>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.
>To post to this group, send email to django...@googlegroups.com.
>Visit this group at http://groups.google.com/group/django-users.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/django-users/81d42f98-04da-4e8f-8c0b-3ac
>86601e116%40googlegroups.com
><https://groups.google.com/d/msgid/django-users/81d42f98-04da-4e8f-8c0b-3a
>c86601e116%40googlegroups.com?utm_medium=email&utm_source=footer> .
>For more options, visit https://groups.google.com/d/optout.


C. Kirby

unread,
May 20, 2014, 7:26:29 PM5/20/14
to django...@googlegroups.com
I would strongly suggest you use signals.
Basically, you would set up a post save signal so that every time a  Song is created you run a function to generate the Word model instances.
One thing to beware of is that signals are still synchronous in the request path - if you are going to be lots of processing then I would put the Word generation function into an asynchronous task in something like celery.
All of this assumes you need to do it in close to real time. If you are ok doing it in a batch-esque way then you can create a management task and cron job (or a celery periodic task) to check for new songs and then generate the Words.

Kirby

Chansons

unread,
May 21, 2014, 3:02:33 PM5/21/14
to django...@googlegroups.com, ilya.ka...@jetbrains.com
Thanks a lot, I'll try that.
Hélène

Tom Lockhart

unread,
May 21, 2014, 3:15:17 PM5/21/14
to django...@googlegroups.com
On 2014-05-20, at 12:26 PM, "C. Kirby" <mis...@gmail.com> wrote:

I would strongly suggest you use signals.

This nicely enables an automatic path of execution. But if you also want to execute this code, say, from the admin interface or separately for testing then you may want to package the fundamental code into a method of the Song model, then run that method in the signal handler.

hth

                        - Tom

--
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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

chansonsyiddish

unread,
May 21, 2014, 4:15:06 PM5/21/14
to django...@googlegroups.com
Thanks, Tom,

I'm not sure I need to use signals altogether, as I know when a Song is added. If I make a method in the Song model, I suppose I can import the model and run it in the shell? Or is there a way I could run it directly from the admin site?

Hélène

Tom Lockhart

unread,
May 21, 2014, 5:35:18 PM5/21/14
to django...@googlegroups.com
On 2014-05-21, at 9:15 AM, chansonsyiddish <chanson...@gmail.com> wrote:

Thanks, Tom,

I'm not sure I need to use signals altogether, as I know when a Song is added. If I make a method in the Song model, I suppose I can import the model and run it in the shell? Or is there a way I could run it directly from the admin site?

Yes and yes.

python manage.py shell
>>> from yourapp.models import Song
>>> s = Song.objects.get(title='your title here')
>>> s.make_words()

For the admin interface, look at the actions keyword for admin classes. Something like

class SongAdmin(admin.ModelAdmin):
  …
  actions = ['gen_words']
  …
  def gen_words(self, request, queryset):
    for obj in queryset:
      obj.make_words()
      pass
    return
    gen_words.description='Generate words for selected songs'

To do the same for all songs at once you can implement a management command for the command line or if in the admin GUI you will likely want to implement a GUI button. I'd stayed away from GUI buttons previously but it turns out to be pretty simple once you have one done.

hth

                     - Tom



Hélène




On 21/05/2014 17:15, Tom Lockhart wrote:
On 2014-05-20, at 12:26 PM, "C. Kirby" <mis...@gmail.com> wrote:

I would strongly suggest you use signals.

This nicely enables an automatic path of execution. But if you also want to execute this code, say, from the admin interface or separately for testing then you may want to package the fundamental code into a method of the Song model, then run that method in the signal handler.

hth

                        - Tom



--
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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Please consider the environment before printing this message.
This message may be privileged and/or confidential, and the sender does not waive any related rights and obligations.  Any distribution, use or copying of this message or the information it contains by other than an intended recipient is unauthorized.  If you received this message in error, please immediately advise me by return e-mail or phone.  All information, references, images, programs, source code, or other materials whatsoever contained in, or supplied with, this document are TRADE SECRETS and governed by the Uniform Trade Secrets Act.  User assumes all direct and consequential liabilities and costs that result from any unauthorized disclosure or use of this information.

chansonsyiddish

unread,
May 21, 2014, 6:11:03 PM5/21/14
to django...@googlegroups.com
Thanks a lot, Tom, I think this is just what I need, I will try it asap and see which is better, between the two solutions.

Regards,
Hélène
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/z416rzZ8Ih8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages