Re: forms.Form, models.Model, forms.ModelForm? (self.django)

2016-08-14 Thread Andrew Emory
Thanks so much.  This is exactly the kind of stuff I wanted to hear.  

It was just unclear if form.Form created a model when you define it or if 
it was just for GET data.

So models are for the database and the admin.  You can render them by way 
of forms.ModelForm which is what you would use for client facing POST 
requests?

On Sunday, August 14, 2016 at 6:35:49 PM UTC-5, Alex Heyden wrote:
>
> forms.Form makes front-end widgets and some validation logic. models.Model 
> defines a database interface and wraps it in a Python class. The two have 
> nothing to do with each other aside from vaguely similar APIs for defining 
> the classes.
>
> The intersection of the two is forms.ModelForm, which uses a model to 
> populate form fields.
>
> GET vs POST is a functionality difference. GET should be used where the 
> request could be repeated multiple times and get the same response. POST 
> should be used where a request has meaningful side effects. (If you're 
> saving things to a model from a user perspective, you almost certainly want 
> POST.) There's middle ground between the two where it's a bit of a matter 
> of opinion, but those are the broad strokes.
>
> On Sun, Aug 14, 2016 at 2:04 PM, Andrew Emory <a.c@gmail.com 
> > wrote:
>
>> Would someone explain to me when you would choose one class over the 
>> other? Does forms.Form not create a database? Can't you just render 
>> models.Model as a form?
>>
>>
>> I understand forms.ModelForm is a helper class for creating a form from a 
>> model. But I still don't really understand why you would choose one over 
>> the other, like when?
>>
>>
>> Are forms.Form more for GET requests? And the others are POST?
>>
>>
>> Most of what I am trying to do is serve some forms, have the user POST 
>> some data, and then query the database.
>>
>>
>> Thanks guys.
>>
>> -- 
>> 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 https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com?utm_medium=email_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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7ebd0d58-7aea-4866-97fe-1cd5ebd1654d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: forms.Form, models.Model, forms.ModelForm? (self.django)

2016-08-14 Thread Alex Heyden
forms.Form makes front-end widgets and some validation logic. models.Model
defines a database interface and wraps it in a Python class. The two have
nothing to do with each other aside from vaguely similar APIs for defining
the classes.

The intersection of the two is forms.ModelForm, which uses a model to
populate form fields.

GET vs POST is a functionality difference. GET should be used where the
request could be repeated multiple times and get the same response. POST
should be used where a request has meaningful side effects. (If you're
saving things to a model from a user perspective, you almost certainly want
POST.) There's middle ground between the two where it's a bit of a matter
of opinion, but those are the broad strokes.

On Sun, Aug 14, 2016 at 2:04 PM, Andrew Emory <a.c.em...@gmail.com> wrote:

> Would someone explain to me when you would choose one class over the
> other? Does forms.Form not create a database? Can't you just render
> models.Model as a form?
>
>
> I understand forms.ModelForm is a helper class for creating a form from a
> model. But I still don't really understand why you would choose one over
> the other, like when?
>
>
> Are forms.Form more for GET requests? And the others are POST?
>
>
> Most of what I am trying to do is serve some forms, have the user POST
> some data, and then query the database.
>
>
> Thanks guys.
>
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com?utm_medium=email_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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Bv0ZYUvnf1p-GCa%3DyrHUfGLORQeSDeBbBnUaDCrvdyEPR5wCQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


forms.Form, models.Model, forms.ModelForm? (self.django)

2016-08-14 Thread Andrew Emory


Would someone explain to me when you would choose one class over the other? 
Does forms.Form not create a database? Can't you just render models.Model 
as a form?


I understand forms.ModelForm is a helper class for creating a form from a 
model. But I still don't really understand why you would choose one over 
the other, like when?


Are forms.Form more for GET requests? And the others are POST?


Most of what I am trying to do is serve some forms, have the user POST some 
data, and then query the database.


Thanks guys.

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Pickling of dinamically created models.Model subclasses: "attribute lookup failed", django 1.5

2013-05-21 Thread akaariai
I have finally found the time to work on this issue. There is a patch
in the ticket (https://code.djangoproject.com/ticket/20289), and I
think the patch does solve the regression.

I didn't find a simple test case that works in 1.4 but fails in 1.5.
If anybody knows how to reproduce the original regression that would
be very useful in ensuring the regression is really solved.

 - Anssi


On 19 huhti, 02:06, akaariai  wrote:
> On 18 huhti, 22:07, Greg H  wrote:
>
> > Running into a similar issue on my own project, seems to be when you try an
> > cache a model which has a related model which in turn has a many to many
> > field. So for example, I have an instance of a Student, which has a
> > ForeignKey to Book, which in turn has a ManyToMany to Author. If I try and
> > cache my Student instance, I get that pickling error. In Django 1.4 it
> > worked fine, but not in 1.5.
>
> > Figure out a resolution to this?
>
> The problem is that for an instance of a class to be picklable the
> class must be available as a module level attribute somewhere. For
> dynamically created classes this is usually not true - they aren't
> available at module level. One set of such classes is automatically
> created through models for m2m fields. The automatic through classes
> could explain the ManyToMany problem.
>
> All in all this looks like a regression that should be fixed in 1.5. I
> created a ticket for this, see:https://code.djangoproject.com/ticket/20289
>
>  - Anssi

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Pickling of dinamically created models.Model subclasses: "attribute lookup failed", django 1.5

2013-04-18 Thread akaariai
On 18 huhti, 22:07, Greg H  wrote:
> Running into a similar issue on my own project, seems to be when you try an
> cache a model which has a related model which in turn has a many to many
> field. So for example, I have an instance of a Student, which has a
> ForeignKey to Book, which in turn has a ManyToMany to Author. If I try and
> cache my Student instance, I get that pickling error. In Django 1.4 it
> worked fine, but not in 1.5.
>
> Figure out a resolution to this?

The problem is that for an instance of a class to be picklable the
class must be available as a module level attribute somewhere. For
dynamically created classes this is usually not true - they aren't
available at module level. One set of such classes is automatically
created through models for m2m fields. The automatic through classes
could explain the ManyToMany problem.

All in all this looks like a regression that should be fixed in 1.5. I
created a ticket for this, see: https://code.djangoproject.com/ticket/20289

 - Anssi

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Pickling of dinamically created models.Model subclasses: "attribute lookup failed", django 1.5

2013-04-18 Thread Greg H
Running into a similar issue on my own project, seems to be when you try an 
cache a model which has a related model which in turn has a many to many 
field. So for example, I have an instance of a Student, which has a 
ForeignKey to Book, which in turn has a ManyToMany to Author. If I try and 
cache my Student instance, I get that pickling error. In Django 1.4 it 
worked fine, but not in 1.5.

Figure out a resolution to this?

On Tuesday, March 19, 2013 10:17:46 PM UTC-7, Sir Anthony wrote:
>
> Hello,
> I'm using 
> django-audit-log<https://github.com/SirAnthony/django-audit-log>witch 
> actively creates dynamic models by calling type('%sAuditLogEntry' 
> % meta_name, (models.Model,), 
> attrs)<https://github.com/SirAnthony/django-audit-log/blob/master/audit_log/models/managers.py#L201>.
>  
> In django 1.5 I getting error when trying to cache it (pickling phase). 
> Example error string: Can't pickle  'testproject.models.TestItemAuditLogEntry'>: attribute lookup 
> testproject.models.TestItemAuditLogEntry failed. I think it because pickle 
> tries to use class name with current environment.
> I looked in django sources and found that this 
> commit<https://github.com/django/django/commit/146aff3bac974e56ec8cb597c2720d1cd4f77b26>is
>  cause of such behaviour. 
> I'm not sure it is django bug (I'll write a ticket with test case and 
> probably patch, instead), maybe I just need to made some hacks on my side, 
> like implement intermediate model with custom __reduce__ like in 1.4 django 
> or something alike.
>
>
>

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Pickling of dinamically created models.Model subclasses: "attribute lookup failed", django 1.5

2013-03-20 Thread Sir Anthony
Hello,
I'm using django-audit-log 
<https://github.com/SirAnthony/django-audit-log>witch actively creates dynamic 
models by calling type('%sAuditLogEntry' 
% meta_name, (models.Model,), 
attrs)<https://github.com/SirAnthony/django-audit-log/blob/master/audit_log/models/managers.py#L201>.
 
In django 1.5 I getting error when trying to cache it (pickling phase). 
Example error string: Can't pickle : attribute lookup 
testproject.models.TestItemAuditLogEntry failed. I think it because pickle 
tries to use class name with current environment.
I looked in django sources and found that this 
commit<https://github.com/django/django/commit/146aff3bac974e56ec8cb597c2720d1cd4f77b26>is
 cause of such behaviour. 
I'm not sure it is django bug (I'll write a ticket with test case and 
probably patch, instead), maybe I just need to made some hacks on my side, 
like implement intermediate model with custom __reduce__ like in 1.4 django 
or something alike.


-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: models.Model

2011-10-18 Thread Daniel Roseman
On Monday, 17 October 2011 15:00:09 UTC+1, youpsla wrote:
>
> Hello Daniel
> thanks for your answer and sorry for my english. As you said, it could be a 
> problem of translation !!! :-)
>
> I really appreciate your help, I'm doing lots of progress in my python 
> object programming curve ...
>
> OK, then to be sure how Python works ... let see this code:
>
> 1 from django.db import models 
> 2 
> 3 class Person(models.Model): 
> 4 first_name = models.CharField(max_length=30) 
> 5 last_name = models.CharField(max_length=30) 
>
> Ligne 1 imports module "models". I've browse the source code of Django. In 
> the "models" folder, there is a __init__.py file and a lots of .py other 
> files.
> There is folders too and specially one called "fields".
>
> Here is my question :
>
>- When importing models on line 1, doest it import all the .py files in 
>this module, then  all classes, Class, functions, variables at the top 
> level 
>of each .py are available for use ? Or there is only an automatic import 
> of 
>the __init__.py ?
>
> Only the __init__.py. But... 

Line 4 : : The CharField Class definition is in models/fields/__init__.py. 
> In the code above, there nowhere a reference at "fields". But the line 10 of 
> the the __init__.py in models is " This file is automatically loaded by 
> Python. In this file on line 10, there is "from django.db.models.fields 
> import *". "
>
> Here is my question :
>
>- Does it means that the CharField Class is available inside Person 
>Class because there is a cascading import following this way:
>   - models contains an __init__.py wich import fields and __init.py in 
>   fields has a definition of Class CharField on line 601
>
>
Yes, this is exactly right.
 

> Hope it's enugh clear ... don't spend much time when it is not and just ask 
> for reformulation, I'll dot it ...
> Regards
>
> Alain
>
>
>
You might like to read the effbot's explanation of the different import 
methods:
http://effbot.org/zone/import-confusion.htm

--
DR. 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/AVl4aUs9D-oJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: models.Model

2011-10-17 Thread youpsla
Hello Daniel
thanks for your answer and sorry for my english. As you said, it could be a 
problem of translation !!! :-)

I really appreciate your help, I'm doing lots of progress in my python 
object programming curve ...

OK, then to be sure how Python works ... let see this code:

1 from django.db import models 
2 
3 class Person(models.Model): 
4 first_name = models.CharField(max_length=30) 
5 last_name = models.CharField(max_length=30) 

Ligne 1 imports module "models". I've browse the source code of Django. In 
the "models" folder, there is a __init__.py file and a lots of .py other 
files.
There is folders too and specially one called "fields".

Here is my question :

   - When importing models on line 1, doest it import all the .py files in 
   this module, then  all classes, Class, functions, variables at the top level 
   of each .py are available for use ? Or there is only an automatic import of 
   the __init__.py ?


Line 4 : : The CharField Class definition is in models/fields/__init__.py. 
In the code above, there nowhere a reference at "fields". But the line 10 of 
the the __init__.py in models is " This file is automatically loaded by 
Python. In this file on line 10, there is "from django.db.models.fields 
import *". "

Here is my question :

   - Does it means that the CharField Class is available inside Person Class 
   because there is a cascading import following this way:
  - models contains an __init__.py wich import fields and __init.py in 
  fields has a definition of Class CharField on line 601
   
Hope it's enugh clear ... don't spend much time when it is not and just ask 
for reformulation, I'll dot it ...
Regards

Alain


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/WBCgsHwa0e0J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: models.Model

2011-10-17 Thread Daniel Roseman


On Monday, 17 October 2011 12:18:00 UTC+1, youpsla wrote:
>
> hello again, sorry for eventually annoying. But later in the Django 
> tutorial, 
>
> there is this code: 
>
> from django.conf.urls import patterns, include, url 
> . 
> urlpatterns = patterns('', 
> (r'^polls/$', 'polls.views.index'), 
> ..)), 
> ) 
>
> What I understand here is that "patterns", "include" and "url" modules 
> are imported. 
>
> After the result of the function "patterns" is assigned to 
> "urlpatterns". AS Daniel said that a function can't be imported, this 
> means that a method can be called (but not imported) or this means 
> that a function and a method are not the same thing ? 
>
> Regards 
>
> Alain 
>

I didn't say that a function can't be imported. It definitely can. As you 
say, function and method aren't the same thing - a method is a function that 
belongs to a class. It's not possible to import just a method, because 
there's no way of referring to it except via the class. But anything at the 
base level of a module - whether it's a class, a function, a variable, 
anything - *can* be imported.
--
DR. 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/hRGc9Z5seJkJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: models.Model

2011-10-17 Thread Daniel Roseman
On Monday, 17 October 2011 12:04:41 UTC+1, youpsla wrote:
>
> Thanks you 2 for your answers. 
>
> I think I've understood now. Again I apologize for the newby Python 
> question but I've read three times "Dive in Python" and it hasn't made 
> me thnigs clear. That's the reason why I've posted. 
>
> I thought there was a hierarchy like this models.Model.CharField  
>
> That's the reason why I didn't understood. 
>
> Daniel, you said, that a method can be imported, this mean that an 
> external method (method from a class like Model) can't be used 
> directly in the Person class for example ? 
>
> If I understand well, in "first_name = 
> models.CharField(max_length=30)", first_name is an instance of class 
> CharField. This instance send "30" to the "max_length" parameter of 
> class CharField. This parameter is used by an internal method of class 
> CharFiled. Is that right ? :-) 
>
> Thanks again to all 
>
> Regards 
>
> Alain 
>

I'm having a little difficulty understanding your question (problème de 
traduction, sans aucun doute). Person inherits from models.Model, so all 
methods that are defined in the superclass are also available to the 
subclass - that's how Person gets methods such as `save`.

As for `max_length=30`, when you instantiate a class Python calls the 
`__init__` method of that class, which in this case accepts a number of 
arguments including max_length.
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/EsB6mXcOqjQJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: models.Model

2011-10-17 Thread youpsla
hello again, sorry for eventually annoying. But later in the Django
tutorial,

there is this code:

from django.conf.urls import patterns, include, url
.
urlpatterns = patterns('',
(r'^polls/$', 'polls.views.index'),
..)),
)

What I understand here is that "patterns", "include" and "url" modules
are imported.

After the result of the function "patterns" is assigned to
"urlpatterns". AS Daniel said that a function can't be imported, this
means that a method can be called (but not imported) or this means
that a function and a method are not the same thing ?

Regards

Alain

On 17 oct, 08:53, Webb Newbie <new...@avatarific.com> wrote:
> "models" is a module that contains classes Model and CharField.
>
> Person inherits from Model.
>
> first_name and last_name are instances of CharField.
>
> There's no functional programming here.
>
> -WN.
>
>
>
>
>
>
>
> On Sun, Oct 16, 2011 at 6:43 PM, youpsla <youp...@gmail.com> wrote:
> > 1 from django.db import models
> > 2
> > 3 class Person(models.Model):
> > 4     first_name = models.CharField(max_length=30)
> > 5     last_name = models.CharField(max_length=30)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: models.Model

2011-10-17 Thread youpsla
Thanks you 2 for your answers.

I think I've understood now. Again I apologize for the newby Python
question but I've read three times "Dive in Python" and it hasn't made
me thnigs clear. That's the reason why I've posted.

I thought there was a hierarchy like this models.Model.CharField 

That's the reason why I didn't understood.

Daniel, you said, that a method can be imported, this mean that an
external method (method from a class like Model) can't be used
directly in the Person class for example ?

If I understand well, in "first_name =
models.CharField(max_length=30)", first_name is an instance of class
CharField. This instance send "30" to the "max_length" parameter of
class CharField. This parameter is used by an internal method of class
CharFiled. Is that right ? :-)

Thanks again to all

Regards

Alain



On 17 oct, 08:53, Webb Newbie <new...@avatarific.com> wrote:
> "models" is a module that contains classes Model and CharField.
>
> Person inherits from Model.
>
> first_name and last_name are instances of CharField.
>
> There's no functional programming here.
>
> -WN.
>
>
>
>
>
>
>
> On Sun, Oct 16, 2011 at 6:43 PM, youpsla <youp...@gmail.com> wrote:
> > 1 from django.db import models
> > 2
> > 3 class Person(models.Model):
> > 4     first_name = models.CharField(max_length=30)
> > 5     last_name = models.CharField(max_length=30)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: models.Model

2011-10-17 Thread Webb Newbie
"models" is a module that contains classes Model and CharField.

Person inherits from Model.

first_name and last_name are instances of CharField.

There's no functional programming here.

-WN.

On Sun, Oct 16, 2011 at 6:43 PM, youpsla <youp...@gmail.com> wrote:

> 1 from django.db import models
> 2
> 3 class Person(models.Model):
> 4 first_name = models.CharField(max_length=30)
> 5 last_name = models.CharField(max_length=30)
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: models.Model

2011-10-17 Thread Daniel Roseman
On Sunday, 16 October 2011 23:43:56 UTC+1, youpsla wrote:
>
> Hello, 
> I'm new to Django and I've only a medium background in funtionnal 
> programming in Python. 
>
> I'm currently reading and working on the Django tutorial. I've few 
> questions on a example of code because things are not so clear for me. 
>
> Here is the code example 
>
> 1 from django.db import models 
> 2 
> 3 class Person(models.Model): 
> 4 first_name = models.CharField(max_length=30) 
> 5 last_name = models.CharField(max_length=30) 
>
> Can you say me if I'm wrong or not on the followings: 
>
> - line 1 : Import of the" models" method from the "django.db" 
> module ? 
>
> - line 3 : Définition of the "Person" classs wich heritates from the 
> "models.Model class ? If true, and assuming that "models" is a method, 
> a class can be nested in a method ? 
>
> - ligne 4 : Creation of variable "first_name" by calling the method 
> CharField with one argument. But how "CharFiled" is written, isn't it 
> a class ? ANd here we use only "models", then why on line 3 we use 
> "models.Model" ? 
>
> I hope it's not to much confusing  :-) 
>
> Help would be REALLY appreciated !!! 
>
> Regards 
>
> Alain



This is fairly basic stuff - sounds like you would benefit from doing an 
introductory Python tutorial.

At the root of your confusion, I think, is that you're mixing up methods and 
modules. `models` in the code above is a module[*] containing multiple 
classes. You can't import a method, as that's simply a function belonging to 
a class. You can import modules - `from django.db import models` - or 
elements such as classes and constants directly inside a module - `from 
django.db.models import Model`.

So, the `models` module includes a class named Model, and also several other 
classes which represent fields - CharField, BooleanField, etc. All of these 
live directly inside the models namespace, so are referred to in exactly the 
same way - models.Model, models.CharField, etc.
--
DR.

[*] Yes, strictly speaking it's a package, but that's probably too confusing 
at this stage

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/m39fNeqPf_YJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



models.Model

2011-10-16 Thread youpsla
Hello,
I'm new to Django and I've only a medium background in funtionnal
programming in Python.

I'm currently reading and working on the Django tutorial. I've few
questions on a example of code because things are not so clear for me.

Here is the code example

1 from django.db import models
2
3 class Person(models.Model):
4 first_name = models.CharField(max_length=30)
5 last_name = models.CharField(max_length=30)

Can you say me if I'm wrong or not on the followings:

- line 1 : Import of the" models" method from the "django.db"
module ?

- line 3 : Définition of the "Person" classs wich heritates from the
"models.Model class ? If true, and assuming that "models" is a method,
a class can be nested in a method ?

- ligne 4 : Creation of variable "first_name" by calling the method
CharField with one argument. But how "CharFiled" is written, isn't it
a class ? ANd here we use only "models", then why on line 3 we use
"models.Model" ?

I hope it's not to much confusing  :-)

Help would be REALLY appreciated !!!

Regards

Alain

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Multiple inheritance from models.Model and admin.ModelAdmin

2011-05-31 Thread Lee
Discovered that save_model() is still available even if I don't
inherit admin.ModelAdmin, which avoids the metaclass conflict.

On May 31, 10:36 am, Lee <lhughe...@gmail.com> wrote:
> I'm trying to create a class that inherits from both models.Model and
> admin.ModelAdmin, so that I can use models.ManyToManyField and
> save_model() from admin.ModelAdmin. Unfortunately this combination
> gives "metaclass conflict: the metaclass of a derived class must be a
> (non-strict) subclass of the metaclasses of all its bases". I've
> googled on the error message and tried a couple of the suggestions but
> with no luck. Anyone know the correct way of doing this?
>
> Thanks for any help.
>
> Lee

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Multiple inheritance from models.Model and admin.ModelAdmin

2011-05-31 Thread Lee
I'm trying to create a class that inherits from both models.Model and
admin.ModelAdmin, so that I can use models.ManyToManyField and
save_model() from admin.ModelAdmin. Unfortunately this combination
gives "metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the metaclasses of all its bases". I've
googled on the error message and tried a couple of the suggestions but
with no luck. Anyone know the correct way of doing this?

Thanks for any help.

Lee

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Subclassing models.Model to extend base functionalities and attributes

2010-05-11 Thread thierry
No, I don't talk about model inheritance. Mainly because base class
fields are propagated to subclasses. I'd like to overload some base
methods of the "models.Model" class to making them available from all
classes of a model.
But a metaclass mechanism makes overloading impossible because it
forces directly to a database model inheritance.

Consequently, I will abadon this way and focus on signal dispatcher
because it seems possible to trigger homemade functions happening
before or after models.Model methods execution. But I stay tuned if
somebody has got a solution.

Regards,

Thierry.



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Subclassing models.Model to extend base functionalities and attributes

2010-05-07 Thread zinckiwi
Are you asking about factoring out certain common functionality shared
between models? It's quite common; for example most of my projects
have something like this as a starting point for many models:

class DatestampedModel(models.Model):
created = models.DateTimeField(default=datetime.now, editable=False)
modified = ModifiedField(editable=False)

class Meta:
abstract = True
ordering = ('-created',)

class MyActualModel(DatestampedModel)
...

Note the difference between abstract base classes and multi-table
inheritance:

http://docs.djangoproject.com/en/1.1/topics/db/models/#id6

Regards
Scott


On May 7, 7:08 am, thierry <thierry.bri...@gmail.com> wrote:
> Hi everybody,
>
> Is there a way to subclass the 'models.Model' class behavior to
> extend
> base functionnalities and attributes to all classes contained in an
> application model ? It seems that the metaclass mechanism force the
> Python inheritance notation to a Database model inheritance.
>
> Thanks,
>
> Thierry.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Subclassing models.Model to extend base functionalities and attributes

2010-05-07 Thread thierry
Hi everybody,

Is there a way to subclass the 'models.Model' class behavior to
extend
base functionnalities and attributes to all classes contained in an
application model ? It seems that the metaclass mechanism force the
Python inheritance notation to a Database model inheritance.

Thanks,

Thierry.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: constraint unique with models.Model

2009-12-27 Thread Marc Aymerich
On Mon, Dec 28, 2009 at 5:15 AM, Karen Tracey <kmtra...@gmail.com> wrote:

> On Sun, Dec 27, 2009 at 11:08 PM, Marc Aymerich <glicer...@gmail.com>wrote:
>
>> hi guys!
>>
>> I'm writing my first project with django and I have doubts in how to
>> declare a constraint unique for two fields of the same table using
>> models.Model.
>>
>
> See:
>
> http://docs.djangoproject.com/en/1.1/ref/models/options/#unique-together
>
> which is a Meta option:
>
> http://docs.djangoproject.com/en/1.1/topics/db/models/#meta-options
>
> Karen
>

Thank you very much Karen!!  :)

If someone serves...

class virtual_aliases(models.Model):
domain = models.ForeignKey(domains)
 source = models.CharField(max_length=50)
destination = models.CharField(max_length=60)
 class Meta:
unique_together = ("source", "domain")

>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Re: constraint unique with models.Model

2009-12-27 Thread Karen Tracey
On Sun, Dec 27, 2009 at 11:08 PM, Marc Aymerich <glicer...@gmail.com> wrote:

> hi guys!
>
> I'm writing my first project with django and I have doubts in how to
> declare a constraint unique for two fields of the same table using
> models.Model.
>

See:

http://docs.djangoproject.com/en/1.1/ref/models/options/#unique-together

which is a Meta option:

http://docs.djangoproject.com/en/1.1/topics/db/models/#meta-options

Karen

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




constraint unique with models.Model

2009-12-27 Thread Marc Aymerich
hi guys!

I'm writing my first project with django and I have doubts in how to declare
a constraint unique for two fields of the same table using models.Model. In
MySQL would be something like:

CREATE TABLE `virtual_aliases` (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
domainid INT(11) NOT NULL,
 source VARCHAR(40) NOT NULL,
destination VARCHAR(80) NOT NULL,
CONSTRAINT UNIQUE_EMAIL UNIQUE (domainid,source),
 FOREIGN KEY (domainid) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE = InnoDB;

with django I only do that:

class virtual_aliases(models.Model):
domain = models.ForeignKey(virtual_domains)
source = models.CharField(max_length=50)
 destination = models.CharField(max_length=60)

I haven't any idea how to declare a unique constraint between 'source' and
'domain'.

Any help¿¿

Thanks a lot!
Marc

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Re: Determine subclass of object in models.Model

2009-02-07 Thread Frank Becker

2009/2/7 Frank Becker <adol...@googlemail.com>:

Hi,


> My question: I have a CommonInfo(models.Model) and I do subclassing it for a
> couple of models. Getting CommonInfo.objects.all() returns a list of all
> objects of CommonInfo and it's subclasses. Q: How to determine what subclass 
> the
> object was instantiated of?

Stupid me. Now I found:
http://groups.google.com/group/django-users/browse_thread/thread/52f72cffebb705e/b76c9d8c89a5574f?lnk=gst=vietnam#b76c9d8c89a5574f

Thanks,

Frank

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Determine subclass of object in models.Model

2009-02-07 Thread Frank Becker

Hi,

First of all thanks for the Django framework. It's fun to work with it.

My question: I have a CommonInfo(models.Model) and I do subclassing it for a
couple of models. Getting CommonInfo.objects.all() returns a list of all
objects of CommonInfo and it's subclasses. Q: How to determine what subclass the
object was instantiated of?

Example:
my models.py contains:

class CommonInfo(models.Model):
name = models.CharField(max_length=100)
age = models.PositiveIntegerField()

class Student(CommonInfo):
home_group = models.CharField(max_length=5)

class Teacher(CommonInfo):
level = models.CharField(max_length=5)

>>> ci = CommonInfo(name="commoninfo", age=1)
>>> student = Student(name="student", age=2, home_group="AAA")
>>> teacher = Teacher(name="teacher", age=3, level="B")
[... .save() all objects   ...]

>>> for object in CommonInfo.objects.all():
...if hasattr(object, 'home_group'):
...print "found student"

Here I expected to find one object or say the if to print "found
student" one time.


Given the pure Python example:

module aaa.py:

class Foo(object):
objects = []
def __init__(self):
self.objects.append(self)

class Bar(Foo):
fnord = 5

I can do:
>>> foo = Foo()
>>> bar = Bar()
>>> Foo.objects
[, ]
>>> for object in Foo.objects:
... if hasattr(object, 'fnord'):
... print "bar"
...
bar

So, even if that might not be possible considering the SQL-DB behind
the ORM: What is the best way to determine the sub class?

Thanks,

Frank

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: bug in models.Model __str__ return and non-ascii characters !?

2009-01-08 Thread adrian

thank you very much. i am using version 1.0.2 right now.
i think i started with django at version 0.95. that time they used
__str__ (i just checked that in my local-copy-archive of the docs for
0.95/0.96) in the tutorial - and i kept that until now.

so with __unicode__ instead of __str__ it works the way i expected it.
thank you very much for your help!



On Jan 9, 1:12 am, "Karen Tracey" <kmtra...@gmail.com> wrote:
> On Thu, Jan 8, 2009 at 6:14 PM, adrian <adrian.imm...@gmail.com> wrote:
>
> > hi,
> > i am posting here to follow the "i found a bug" guide ...
>
> > let's assume i have a model like this:
>
> > ---
> > class Test(models.Model):
> >    testname = models.CharField(max_length=60, null=False,
> > blank=False, unique=True)
> >    [... some more fields ...]
> >    def __str__(self):
> >        return self.testname
> > --
>
> > this models works perfectly as long as users only use ascii-characters
> > in the testname field. as soon as a user (in django.admin) enters a
> > german umlaut -for example- an error is thrown like this:
>
> > Caught an exception while rendering: ('ascii', u'German Umlaut\xfc',
> > 20, 21, 'ordinal not in range(128)')
>
> > this error is reproducable with different database backends (i tested
> > sqlite3 and psycopg2) and the model above is reduced to the minimum to
> > reproduce. (probably you could remove null, blank and unique
>
> > i don't know where the error is produced, wether it is when the
> > template uses something like {{Test}} because {{Test.testname}}
> > works ... if 'testname' is used as foreignkey in other models and the
> > admin interface wants to render this as a dropdown with probably
> > something like 

Re: bug in models.Model __str__ return and non-ascii characters !?

2009-01-08 Thread Karen Tracey
On Thu, Jan 8, 2009 at 6:14 PM, adrian <adrian.imm...@gmail.com> wrote:

>
> hi,
> i am posting here to follow the "i found a bug" guide ...
>
> let's assume i have a model like this:
>
> ---
> class Test(models.Model):
>testname = models.CharField(max_length=60, null=False,
> blank=False, unique=True)
>[... some more fields ...]
>def __str__(self):
>return self.testname
> --
>
> this models works perfectly as long as users only use ascii-characters
> in the testname field. as soon as a user (in django.admin) enters a
> german umlaut -for example- an error is thrown like this:
>
> Caught an exception while rendering: ('ascii', u'German Umlaut\xfc',
> 20, 21, 'ordinal not in range(128)')
>
> this error is reproducable with different database backends (i tested
> sqlite3 and psycopg2) and the model above is reduced to the minimum to
> reproduce. (probably you could remove null, blank and unique
>
> i don't know where the error is produced, wether it is when the
> template uses something like {{Test}} because {{Test.testname}}
> works ... if 'testname' is used as foreignkey in other models and the
> admin interface wants to render this as a dropdown with probably
> something like 

bug in models.Model __str__ return and non-ascii characters !?

2009-01-08 Thread adrian

hi,
i am posting here to follow the "i found a bug" guide ...

let's assume i have a model like this:

---
class Test(models.Model):
testname = models.CharField(max_length=60, null=False,
blank=False, unique=True)
[... some more fields ...]
def __str__(self):
return self.testname
--

this models works perfectly as long as users only use ascii-characters
in the testname field. as soon as a user (in django.admin) enters a
german umlaut -for example- an error is thrown like this:

Caught an exception while rendering: ('ascii', u'German Umlaut\xfc',
20, 21, 'ordinal not in range(128)')

this error is reproducable with different database backends (i tested
sqlite3 and psycopg2) and the model above is reduced to the minimum to
reproduce. (probably you could remove null, blank and unique

i don't know where the error is produced, wether it is when the
template uses something like {{Test}} because {{Test.testname}}
works ... if 'testname' is used as foreignkey in other models and the
admin interface wants to render this as a dropdown with probably
something like 

Re: ultra-n00b models.Model syntax error

2008-09-21 Thread Karen Tracey
On Sun, Sep 21, 2008 at 4:21 PM, notatoad <[EMAIL PROTECTED]> wrote:

>
> i just set up django and started on the tutorial.  i get this after
> calling 'python manage.py sql polls'
>
> [snip]
> File "/usr/local/django-apps/mysite/../mysite/polls/models.py", line 3
>
>class Poll(models.Model)
>
> ^SyntaxError: invalid syntax
>
> i'm working with the tutorial code here:
> http://docs.djangoproject.com/en/dev/intro/tutorial01/#id3
>
> any ideas what's gone wrong?
>
>
Assuming the caret is actually pointing at the last character in the
identified line, then you're missing a ':' on the end of that line.

Karen

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



ultra-n00b models.Model syntax error

2008-09-21 Thread notatoad

i just set up django and started on the tutorial.  i get this after
calling 'python manage.py sql polls'

ceback (most recent call last):

File "manage.py", line 11, in ?

execute_manager(settings)

File "/usr/lib/python2.4/site-packages/django/core/management/
__init__.py", line 340, in execute_manager

utility.execute()

File "/usr/lib/python2.4/site-packages/django/core/management/
__init__.py", line 295, in execute

self.fetch_command(subcommand).run_from_argv(self.argv)

File "/usr/lib/python2.4/site-packages/django/core/management/
base.py", line 77, in run_from_argv

self.execute(*args, **options.__dict__)

File "/usr/lib/python2.4/site-packages/django/core/management/
base.py", line 95, in execute

self.validate()

File "/usr/lib/python2.4/site-packages/django/core/management/
base.py", line 122, in validate

num_errors = get_validation_errors(s, app)

File "/usr/lib/python2.4/site-packages/django/core/management/
validation.py", line 28, in get_validation_errors

for (app_name, error) in get_app_errors().items():

File "/usr/lib/python2.4/site-packages/django/db/models/loading.py",
line 128, in get_app_errors

self._populate()

File "/usr/lib/python2.4/site-packages/django/db/models/loading.py",
line 57, in _populate

self.load_app(app_name, True)

File "/usr/lib/python2.4/site-packages/django/db/models/loading.py",
line 72, in load_app

mod = __import__(app_name, {}, {}, ['models'])

File "/usr/local/django-apps/mysite/../mysite/polls/models.py", line 3

class Poll(models.Model)

^SyntaxError: invalid syntax

i'm working with the tutorial code here: 
http://docs.djangoproject.com/en/dev/intro/tutorial01/#id3

any ideas what's gone wrong?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



inheriting a models.Model inherited class

2007-06-06 Thread [EMAIL PROTECTED]

I hope the subject isn't too confusing but what I want to do seems
pretty
common so I think I might be missing something obvious (or should be
doing
in a different way)

class Person(models.Model):
name = models.TextField()


def get_whatever:
pass

class PersonExtra(Person):
def get_whatever2:
pass


p = PersonExtra.objects.get(id=3)
p.get_whatever # fine
p.get_whatever2 # does not exist

type(p) # models.Person

Advice?

Thanks


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: error in subclassing models.Model

2007-04-09 Thread checco

Thanks a lot James,

I've just tried the same modifying an existing file of the cheeserater
source code: to be precise I added the following lines:

class Person(models.Model):
pass

at the end of the file models.py under c:\cheeserater\packages but I
still get the same error

Maybe I misunderstood the previous reply?

Anyway I seem to remember that I didn't have problems, in the past,
making a class definition within the shell (but I could be wrong).

Any further help will be very appreciated.
Francesco

P.S. Just for your information: when I start the development server
the local site (cheeserater) works fine

On Apr 9, 10:56 pm, "James Bennett" <[EMAIL PROTECTED]> wrote:
> On 4/9/07, checco <[EMAIL PROTECTED]> wrote:
>
> > Then, these really basic statements give me this error:
>
> > >>> from django.db import models
> > >>> class Person(models.Model):
> > pass
>
> This is somewhat counterintuitive unless you know a bit about how
> Django's model system works; 'app_label' is an attribute of the model
> class which needs to exist for Django to figure some things out about
> it, and is derived from the location of the file which contains the
> model class definition. As a result, defining a model "on the fly" in
> the Python shell doesn't really work, because there is no "file" in
> which the model gets defined.
>
> --
> "Bureaucrat Conrad, you are technically correct -- the best kind of correct."


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: error in subclassing models.Model

2007-04-09 Thread James Bennett

On 4/9/07, checco <[EMAIL PROTECTED]> wrote:
> Then, these really basic statements give me this error:
>
> >>> from django.db import models
> >>> class Person(models.Model):
> pass

This is somewhat counterintuitive unless you know a bit about how
Django's model system works; 'app_label' is an attribute of the model
class which needs to exist for Django to figure some things out about
it, and is derived from the location of the file which contains the
model class definition. As a result, defining a model "on the fly" in
the Python shell doesn't really work, because there is no "file" in
which the model gets defined.


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



error in subclassing models.Model

2007-04-09 Thread checco

I update the django distribution quite regularly and recently I get an
unexpected error.

Let's suppose I'm using the settings.py file from the cheeserater code
(the django website recently launched with source code available). At
the IDLE startup I write the following:

>>> import os, sys
>>> os.environ['DJANGO_SETTINGS_MODULE'] = 'cheeserater.settings'
>>> sys.path.append("C:\\")
(The cheeserater package is under the C drive.)

Then, these really basic statements give me this error:

>>> from django.db import models
>>> class Person(models.Model):
pass


Traceback (most recent call last):
  File "<pyshell#6>", line 1, in 
class Person(models.Model):
  File "C:\Python25\lib\site-packages\django\db\models\base.py", line
50, in __new__
new_class._meta.app_label = model_module.__name__.split('.')[-2]
IndexError: list index out of range


Can anyone help?

Thanks and regards
Francesco


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: inheriting from models.Model

2006-06-29 Thread spako

for this problem i used the Generic foreign key/relationships. an
example in the documentation:
http://www.djangoproject.com/documentation/models/generic_relations/

although i didn't find anything about it in the Model API.

there is another thing i'd like to do with the above comments example:
what if for video and user comments there is an extra field char field.
and this char field need different choices (i.e.
choices=VIDEO_COMMENT_CHOICES) for video and user comments. is there a
way to fit this in the generic relation way? the way i'd think it would
work is:

class Comment:
CHOICES = ()
comment = models.TextField()
choice = models.charField(choices=CHOICES)

class VideoComment(models.Model, Comment):
CHOICES = (('a', 'Apple'), ('p', 'Pear'))
video = models.ForeignKey(Video)

class UserComment(models.Model, Comment):
CHOICES = (('a', 'Admin'), ('p', 'Public User'))
user = models.ForeignKey(User)

the CHOICES in VideoComment would then be used instead of the blank on
in Comment. (i'm new to python so don't know if this is actually
viable)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: inheriting from models.Model

2006-06-29 Thread knobi

Hi,
some time ago i did something similar and wondered to get some problems
using
the query manager. So when i used

class Comment(models.Model):
class VideoComment(Comment):

and then asked
VideoComment.objects.get(id='xyz')
the Manager tried to to look up the Comment Table

My solution was to explicitely set the default manager inside the
derived class like:

class VideoComment(Comment):
   objects = models.Manager()

besides that there seems to be some discussion on inheritance for not
yet
beeing implemented in the development version


hope this helps

knobi


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: inheriting from models.Model

2006-06-28 Thread James Bennett

On 6/28/06, spako <[EMAIL PROTECTED]> wrote:
> is there a way to achieve this? or should i not bother and just do have
> a Comment Model and just have VideoComment and UserComment have a
> OneToOneField to Comment?

Generally when you want to do something like this you define only one
model, and accomplish the relation to multiple classes by giving it
two fields: one a foreign key to a content-type, the other an object
id. The combination of content-type and object id specifies the exact
object it's tied to, and is generic enough to allow you to tie to any
model in your system (this is exactly how Django's built-in comments
system works).

This has recently been formalized as a "generic foreign key field",
which is being fleshed out.

-- 
"May the forces of evil become confused on the way to your house."
  -- George Carlin

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



inheriting from models.Model

2006-06-28 Thread spako

hi

i'm trying to create a class that contains fields that can be inherited
by Model classes like this (User and Video also inherit from Model):

class Comment:
author = models.ForeignKey(User)
comment = models.TextField()
date_commented = models.DateTimeField(auto_now_add=True)

class VideoComment(models.Model, Comment):
video = models.ForeignKey(Video)

class UserComment(models.Model, Comment):
user = models.ForeignKey(User)


i've also tried a variation, by declarinthe classes like this (the
contents of the classes are the same as above):

class Comment(models.Model):
class VideoComment(Comment):
class UserComment(Comment):

this does not seem right and does not work anyway.

is there a way to achieve this? or should i not bother and just do have
a Comment Model and just have VideoComment and UserComment have a
OneToOneField to Comment?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---