Re: function to create objects in a given model

2014-05-21 Thread chansonsyiddish
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




On 21/05/2014 19:35, Tom Lockhart wrote:
On 2014-05-21, at 9:15 AM, chansonsyiddish > 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" > 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+unsubscr...@googlegroups.com 
.
To post to this group, send email to django-users@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/537CD10A.9070307%40gmail.com 
.

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./*


--
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+unsubscr...@googlegroups.com 
.
To post to this group, send email to django-users@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/75348526-0BC0-4153-B1C9-2DCEA95F3282%40gmail.com 
.

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


--
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/537CEC37.4060502%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: function to create objects in a given model

2014-05-21 Thread Tom Lockhart
On 2014-05-21, at 9:15 AM, chansonsyiddish  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"  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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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/537CD10A.9070307%40gmail.com.
> 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.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/75348526-0BC0-4153-B1C9-2DCEA95F3282%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: function to create objects in a given model

2014-05-21 Thread chansonsyiddish

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




On 21/05/2014 17:15, Tom Lockhart wrote:
On 2014-05-20, at 12:26 PM, "C. Kirby" > 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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/537CD10A.9070307%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: function to create objects in a given model

2014-05-21 Thread Chansons
Thanks a lot, I'll try that.
Hélène


Le mardi 20 mai 2014 20:50:34 UTC+2, Ilya Kazakevich a écrit :
>
> 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!" 
>
>
> >-Original Message- 
> >From: django...@googlegroups.com  
> >[mailto:django...@googlegroups.com ] On Behalf Of Chansons 
> >Sent: Tuesday, May 20, 2014 7:10 PM 
> >To: django...@googlegroups.com  
> >Subject: function to create objects in a given model 
> > 
> >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 
> > 
> > 
> > 
> > 
> >-- 
> >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. 
>
>
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/fbe173a8-cfbe-489c-bc56-e767edc914b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


RE: function to create objects in a given model

2014-05-20 Thread Ilya Kazakevich
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!"


>-Original Message-
>From: django-users@googlegroups.com
>[mailto:django-users@googlegroups.com] On Behalf Of Chansons
>Sent: Tuesday, May 20, 2014 7:10 PM
>To: django-users@googlegroups.com
>Subject: function to create objects in a given model
>
>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
>
>
>
>
>--
>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+unsubscr...@googlegroups.com.
>To post to this group, send email to django-users@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.


-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/08b401cf745c%246253b520%2426fb1f60%24%40JetBrains.com.
For more options, visit https://groups.google.com/d/optout.


Re: function to create objects in a given model

2014-05-20 Thread Chansons
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 :
>
>
> https://docs.djangoproject.com/en/dev/howto/initial-data/
>
> -V 
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/ffcb89bd-74a4-410b-b886-884c1cb920c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: function to create objects in a given model

2014-05-20 Thread Venkatraman S
On Tue, May 20, 2014 at 8:39 PM, Chansons  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?
>
>
https://docs.djangoproject.com/en/dev/howto/initial-data/

-V

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CAN7tdFSjMpE1Mxoy1gs83Bb%2BkDDANtRvsULrbrrhvfiyZiakRQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


function to create objects in a given model

2014-05-20 Thread Chansons
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


-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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-3ac86601e116%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.