Re: re-using field definitions

2009-09-24 Thread Chris Withers

Ethan Jucovy wrote:
> What happens if you wrap the definition in a function?
> {{{
> def signins(): return models.IntegerField(...)
> 
> class User(models.Model):
>  name = models.IntegerField(...)
>  signins = signins()
> }}}

Yeah, this is sort of what I ended up with:

from django.db import models
from functools import partial

signins = partial(models.IntegerField,
 default=0,
 db_index=True,
 verbose_name='Total Signins'
 )

class User(models.Model):
 name = models.CharField(
 max_length=50,
 primary_key=True,
 verbose_name='Username'
 )
 signins = signins()

class Month(models.Model):
 user = models.ForeignKey(User)
 monthname = models.CharField(
 max_length=14,
 db_index=True,
 verbose_name='Month'
 )
 signins = signins()

Partials rock :-)
(I could also set all the common bits in the partial and only have the 
bits that are different in the field definition - DRY to the max :-P)

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: re-using field definitions

2009-09-24 Thread Ethan Jucovy

On Thu, Sep 24, 2009 at 9:37 AM, Chris Withers  wrote:
>
> Hi All,
>
> I tried this:
>
> from django.db import models
>
> signins = models.IntegerField(
>     default=0,
>     db_index=True,
>     verbose_name='Total Signins'
>     )
>
> class User(models.Model):
>     name = models.CharField(
>         max_length=50,
>         primary_key=True,
>         verbose_name='Username'
>         )
>     signins = signins
>
> class Month(models.Model):
>     user = models.ForeignKey(User)
>     monthname = models.CharField(
>         max_length=14,
>         db_index=True,
>         verbose_name='Month'
>         )
>     signins = signins
>
> ...but the effect was to randomise the order of the fields in the admin
> interface :-(
>
> How should I re-use field definitions so I'm not violating DRY but also
> such that field ordering stays consistent?

What happens if you wrap the definition in a function?
{{{
def signins(): return models.IntegerField(...)

class User(models.Model):
 name = models.IntegerField(...)
 signins = signins()
}}}

egj

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