Re: Subclassing Models in non model class

2016-12-28 Thread M Hashmi
Thanks for the reply. I was trying to understand sql level inheritance
difference between Proxy based models and abstract models.

I got it figured out.
Thanks

Regards,
Mudassar

On Wed, Dec 28, 2016 at 4:09 AM, Melvyn Sopacua 
wrote:

> On Tuesday 27 December 2016 05:51:55 M Hashmi wrote:
>
>
>
> > I created a model Address like a normal practice below. Now I need to
>
> > create a subclass without using models.Model. But its not letting me
>
> > do because obviously it won't create tables unless I call
>
> > models.Model in class parameters.
>
> >
>
> > Idea is to create a model and use that model with non db reference in
>
> > admin.py. Following is the code:
>
>
>
> It's quite unclear what you want and why you want it especially. What does
> this other Address model represent?
>
> --
>
> Melvyn Sopacua
>
> --
> 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/5208735.ZJ1U0fmsWf%40devstation
> 
> .
> 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/CANoUts4rinzvF%2BdHfuN8WKof6CjsXWE4O5w-J-MZoB%3DuGS3N6A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Subclassing Models in non model class

2016-12-28 Thread Melvyn Sopacua
On Tuesday 27 December 2016 05:51:55 M Hashmi wrote:

> I created a model Address like a normal practice below. Now I need to
> create a subclass without using models.Model. But its not letting me
> do because obviously it won't create tables unless I call
> models.Model in class parameters.
> 
> Idea is to create a model and use that model with non db reference 
in
> admin.py. Following is the code:

It's quite unclear what you want and why you want it especially. What 
does this other Address model represent?
-- 
Melvyn Sopacua

-- 
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/5208735.ZJ1U0fmsWf%40devstation.
For more options, visit https://groups.google.com/d/optout.


Subclassing Models in non model class

2016-12-27 Thread M Hashmi
Hi All,

I created a model Address like a normal practice below. Now I need to
create a subclass without using models.Model. But its not letting me do
because obviously it won't create tables unless I call models.Model in
class parameters.

Idea is to create a model and use that model with non db reference in
admin.py. Following is the code:

"class Address(models.Model):
 ...
class ShippingAddress(Address):
phone_number = PhoneNumberField(
_("Phone number"), blank=True,
help_text=_("In case we need to call you about your order"))
notes = models.TextField(
blank=True, verbose_name=_('Instructions'),
help_text=_("Tell us anything we should know when delivering "
"your order."))

class Meta:
app_label = 'order'
verbose_name = _("Shipping address")
verbose_name_plural = _("Shipping addresses")

@property
def order(self):
try:
return self.order_set.all()[0]
except IndexError:
return None


class UserAddress(ShippingAddress):
user = models.ForeignKey(
AUTH_USER_MODEL, related_name='addresses', verbose_name=_("User"))
is_default_for_shipping = models.BooleanField(
_("Default shipping address?"), default=False)
is_default_for_billing = models.BooleanField(
_("Default billing address?"), default=False)
num_orders = models.PositiveIntegerField(_("Number of Orders"),
default=0)
hash = models.CharField(_("Address Hash"), max_length=255,
db_index=True,
editable=False)
date_created = models.DateTimeField(_("Date Created"),
auto_now_add=True)

def save(self, *args, **kwargs):
self.hash = self.generate_hash()
self._ensure_defaults_integrity()
super(UserAddress, self).save(*args, **kwargs)

def _ensure_defaults_integrity(self):
if self.is_default_for_shipping:
self.__class__._default_manager\
.filter(user=self.user, is_default_for_shipping=True)\
.update(is_default_for_shipping=False)
if self.is_default_for_billing:
self.__class__._default_manager\
.filter(user=self.user, is_default_for_billing=True)\
.update(is_default_for_billing=False)

class Meta:
app_label = 'address'
verbose_name = _("User address")
verbose_name_plural = _("User addresses")
ordering = ['-num_orders']
unique_together = ('user', 'hash')

def validate_unique(self, exclude=None):
super(Address, self).validate_unique(exclude)
qs = self.__class__.objects.filter(
user=self.user,
hash=self.generate_hash())
if self.id:
qs = qs.exclude(id=self.id)
if qs.exists():
raise exceptions.ValidationError({
'__all__': [_("This address is already in your address"
  " book")]})

.."

I know that I needed to create a parent class without calling models and
then I can inherit that class in models.Model.

But is there a way around it or do I need to create abstract models?

Regards,
Mudassar

-- 
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/CANoUts4wcHLvqF3Q_7cJ%3D3LxTAFM5m-2J7UO%3Dueit1DN4XpF5g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: problems subclassing models

2009-10-09 Thread Chris Withers

Daniel Roseman wrote:
> If your base model doesn't contain any fields, you should probably
> mark it as abstract.
> http://docs.djangoproject.com/en/dev/topics/db/models/#id6

Thanks, knew there'd be some magic I needed to invoke ;-)

Chris


--~--~-~--~~~---~--~~
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: problems subclassing models

2009-10-08 Thread Daniel Roseman

On Oct 8, 4:05 pm, Chris Withers  wrote:
> Hi All,
>
> I have a set of models that all have the same implementation for a
> method, so I thought I'd create a base class for these:
>
> class UrlModel(models.Model):
>      def get_absolute_url(self):
>          return reverse(index,kwargs=dict(fk=self.pk))
>
> ...and then have the relevant models subclass that. However, as soon as
> I did this, I started getting bizarre SQL errors, things like:
>
> ProgrammingError at /some/path
> relation "myapp_urlmodel" does not exist
> LINE 1: ...somefield1"."somefield2" FROM "myapp_modelname" INNER JOIN
> "myapp_urlm...
>
> Why is this?
>
> Chris

If your base model doesn't contain any fields, you should probably
mark it as abstract.
http://docs.djangoproject.com/en/dev/topics/db/models/#id6
--
DR.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



problems subclassing models

2009-10-08 Thread Chris Withers

Hi All,

I have a set of models that all have the same implementation for a 
method, so I thought I'd create a base class for these:

class UrlModel(models.Model):
 def get_absolute_url(self):
 return reverse(index,kwargs=dict(fk=self.pk))

...and then have the relevant models subclass that. However, as soon as 
I did this, I started getting bizarre SQL errors, things like:

ProgrammingError at /some/path
relation "myapp_urlmodel" does not exist
LINE 1: ...somefield1"."somefield2" FROM "myapp_modelname" INNER JOIN 
"myapp_urlm...

Why is this?

Chris

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

--~--~-~--~~~---~--~~
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

2007-02-10 Thread Malcolm Tredinnick

On Sat, 2007-02-10 at 23:22 +, Zach Seifts wrote:
> I don't want to use a ForeignKey. I want to be able to set up a class
> in the model and not have a table created. Maybe the base model thing
> is the way to do it. I've read somewhere about mixins, could these be
> of interest?

As Collin mentioned, true model inheritance in Django (even with
abstract base classes as you are doing here) does not work yet. The
reason is that although it works on the Python level, you end up with
the wrong model manager and the multi-table queries are not constructed
correctly. A fix for this will land shortly (see [1]).

For what you are trying to do, you might be able to get away with not
making Foo a subclass of models.Model and then making Bar inherit from
both models.Model and Foo, although I'm not 100% sure that will work
correctly, either. But it should be fairly quick to test.

Sorry we cannot help you with a perfect solution. This is completely my
fault -- I have been dragging the chain on this one. It will be fixed
soon.

[1] http://code.djangoproject.com/wiki/VersionOneFeatures

Regards,
Malcolm


--~--~-~--~~~---~--~~
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: Subclassing models

2007-02-10 Thread Zach Seifts

I don't want to use a ForeignKey. I want to be able to set up a class
in the model and not have a table created. Maybe the base model thing
is the way to do it. I've read somewhere about mixins, could these be
of interest?

On Feb 10, 4:37 pm, "Collin Grady" <[EMAIL PROTECTED]> wrote:
> Subclassing doesn't work right yet, you need to use a ForeignKey or
> similar to the base model in order to relate them - if you do it the
> way you're trying it will break things randomly.
>
> On Feb 10, 12:25 pm, "Zach Seifts" <[EMAIL PROTECTED]> wrote:
>
> > I have been working on a project where I need to create a few classes
> > in the model that are very similar. What I want to do is subclass a
> > model but have it not set up a database table. I know there are ways
> > of doing this I just haven't figured out yet.
>
> > Here's an example of what I am trying to do
>
> > class Foo(models.Model)
> > """ This class does not have a db table """
> > name = models.CharField(maxlength=100)
>
> > class Bar(Foo)
> > """ This class has a db table """
> > eyeColor = models.CharField(maxlength=100)
>
> > Thanks for you help.


--~--~-~--~~~---~--~~
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: Subclassing models

2007-02-10 Thread Collin Grady

Subclassing doesn't work right yet, you need to use a ForeignKey or
similar to the base model in order to relate them - if you do it the
way you're trying it will break things randomly.

On Feb 10, 12:25 pm, "Zach Seifts" <[EMAIL PROTECTED]> wrote:
> I have been working on a project where I need to create a few classes
> in the model that are very similar. What I want to do is subclass a
> model but have it not set up a database table. I know there are ways
> of doing this I just haven't figured out yet.
>
> Here's an example of what I am trying to do
>
> class Foo(models.Model)
> """ This class does not have a db table """
> name = models.CharField(maxlength=100)
>
> class Bar(Foo)
> """ This class has a db table """
> eyeColor = models.CharField(maxlength=100)
>
> Thanks for you help.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Subclassing models

2007-02-10 Thread Zach Seifts

I have been working on a project where I need to create a few classes
in the model that are very similar. What I want to do is subclass a
model but have it not set up a database table. I know there are ways
of doing this I just haven't figured out yet.

Here's an example of what I am trying to do

class Foo(models.Model)
""" This class does not have a db table """
name = models.CharField(maxlength=100)

class Bar(Foo)
""" This class has a db table """
eyeColor = models.CharField(maxlength=100)

Thanks for you help.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Subclassing models

2006-02-19 Thread [EMAIL PROTECTED]

Hello:

I am trying to extend the admin interface to display custom objects.
Specifically, I would like use the list view (from the change_list
function) to show the results of custom queries.

The two options that I have come up with is to:

* subclass the ChangeList object and support other objects besides the
model object.  This seems like the obvious approach - but it was a
little diffiicult understanding all the dependencies the change_list
object requires

* subclass the django model object so that I can basically create
custom queries utilizing the existing model apis.  I would not have to
modify the change_list object.  I tried this - but the problem  is that
 the subclass creates new tables - which is not really what I want.  Is
there a way to create a subclass of the django model object but have
the subclass and the parent class share the same table space (and have
the managment app create the correct sql for the sqlreset and othe
commands)?  Not sure this is really the "best" way - kind of seems like
a hack.

Anyone have any opinion on these options or others that I may be
missing?


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---