Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » abstract base class and overwriting save() [RSS Feed]

#1 Jan. 28, 2011 09:21:56

Jaroslav D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

abstract base class and overwriting save()


Hi,

if I have got several models that have identical code, except for
their save method, how can I use an abstract base class for them? Will
I have to overwrite save() for each model? Or is there a generic way
to define save(), i.e. a way that I have to use only once -- in the
abstract base class?

class BMW(models.Model):
...
save def save(self, *args, **kwargs):
do_something()
super(BMW, self).save(*args, **kwargs)

class Fiat(models.Model):
...
save def save(self, *args, **kwargs):
do_something()
super(Fiat, self).save(*args, **kwargs)

Jaroslav

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 28, 2011 09:36:36

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

abstract base class and overwriting save()


On Friday, January 28, 2011 9:21:48 AM UTC, Jaroslav Dobrek wrote:
>
> Hi,
>
> if I have got several models that have identical code, except for
> their save method, how can I use an abstract base class for them? Will
> I have to overwrite save() for each model? Or is there a generic way
> to define save(), i.e. a way that I have to use only once -- in the
> abstract base class?
>
> class BMW(models.Model):
> ...
> save def save(self, *args, **kwargs):
> do_something()
> super(BMW, self).save(*args, **kwargs)
>
> class Fiat(models.Model):
> ...
> save def save(self, *args, **kwargs):
> do_something()
> super(Fiat, self).save(*args, **kwargs)
>
> Jaroslav


The whole point of subclassing is that subclasses inherit models from the
superclass. That's how the model save() method is definted in the first
place, if you don't override it. What exactly are you having problems with?
--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 28, 2011 09:43:30

Jaroslav D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

abstract base class and overwriting save()


> The whole point of subclassing is that subclasses inherit models from the
> superclass. That's how the model save() method is definted in the first
> place, if you don't override it. What exactly are you having problems with?

It seems that my problems were due to another mistake of mine. I was
thinking in a too complicated way. You are right: nothing has to be
done, just inherit. Thank you.

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 28, 2011 09:57:39

Chris M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

abstract base class and overwriting save()


The "save def save(self, *args, **kwargs):" should not have the leading save.

Also, if you are making a car system then this structure seems more appropriate:
class CarMake(models.Model):
...


class Car(models.Model):
make = models.ForeignKey(CarMaker)
...
def save(self, *args, **kwargs):
do_something()
super(Car, self).save(*args, **kwargs)



From: django-users@googlegroups.com On
Behalf Of Daniel Roseman
Sent: 28 January 2011 11:36
To: django-users@googlegroups.com
Subject: Re: abstract base class and overwriting save()

On Friday, January 28, 2011 9:21:48 AM UTC, Jaroslav Dobrek wrote:
Hi,

if I have got several models that have identical code, except for
their save method, how can I use an abstract base class for them? Will
I have to overwrite save() for each model? Or is there a generic way
to define save(), i.e. a way that I have to use only once -- in the
abstract base class?

class BMW(models.Model):
...
save def save(self, *args, **kwargs):
do_something()
super(BMW, self).save(*args, **kwargs)

class Fiat(models.Model):
...
save def save(self, *args, **kwargs):
do_something()
super(Fiat, self).save(*args, **kwargs)

Jaroslav

The whole point of subclassing is that subclasses inherit models from the
superclass. That's how the model save() method is definted in the first place,
if you don't override it. What exactly are you having problems with?
--
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 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-users@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.

Offline

  • Root
  • » Django
  • » abstract base class and overwriting save() [RSS Feed]

Board footer

Moderator control

Enjoy the 12th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support