views:

77

answers:

4

Now I have this code:

class Mymodel(models.Model):
    xxxx_count = ForeignCountField(filter={'foreign_table__xxxx': True})
    yyyy_count = ForeignCountField(filter={'foreign_table__yyyy': True})
    zzzz_count = ForeignCountField(filter={'foreign_table__zzzz': True})
    qqqq_count = ForeignCountField(filter={'foreign_table__qqqq': True})
    ssss_count = ForeignCountField(filter={'foreign_table__ssss': True})
    rrrr_count = ForeignCountField(filter={'foreign_table__rrrr': True})

I want something like this:

class Mymodel(models.Model):
    for code in ['xxxx','yyyy','zzzz','qqqq','ssss','rrrr']:
        setattr(self, '%s_count' % code, ForeignCountField(filter={'foreign_table__%s' % code: True}))

But when I try to do this, error raised: "self doesn't defined". Do I need to put this code into some other place?

A: 

Maybe you need to put you're code in the constructor method init of your class.

def __init__(self, *args, **kwargs):
    .....
    super(MyModel, self).__init__(*args, **kwargs)

Edit: Sorry this don't work. Maybe you can try looking at This, 'this works with django 0.96. There are some modification that make it hard to adjust it with django version 1.0'.

if you wan't it Dirty and Quick you can try something like this:

class Foo(models.Model):
    letters = ["A", "B", "C"]
    for l in letters:
        exec "%s_count = models.CharField(max_length=255)" % l
Andrea Di Persio
what the difference between defining inside __init__() or outside __init__(). Only definiteness of self variable?
ramusus
Yes, in __init__ self exist, outside not.
Andrea Di Persio
I updated my reply.
Andrea Di Persio
I try to do that, but my schema evolution app: "deseb" suggests to me drops all this fields. In shell, print says they are <utils.composition.ForeignCount object at ....>, but before it they were PositiveInteger. I think it doesn't work correctly.
ramusus
Ooh, just saw your edit. Thanks for dirty method, but I will try to find more convenient. I saw DynamicModels before, and at that time I think, that defining this fields without loop is better then doing so many changes with model.
ramusus
+1  A: 

The way to automate the creation of multiple class attributes is by writing a metaclass. It'd certainly be possible to subclass Django's ModelBase metaclass and add the functionality you need; but it would also be overkill and probably less maintainable than just writing them out.

Carl Meyer
+1  A: 

I'd like to know a bit more about the design decisions that got you to this point. What is a ForeignCountField and why do you need it? Shouldn't it be a property of the respective tables? (a method on the manager or a classmethod?)

andybak
Excellent question. I almost asked this too, but figured that someone who went to the trouble of writing a ForeignCountField probably knows what they're doing and needs the denormalization.
Carl Meyer
I have 6 denormalization fields, that builded by the same principle and I want to have a more scalable method to support them. I feel 7th same field can appear every moment
ramusus
+1  A: 

If you want to add fields outside model definition you have to use add_to_class model method:

class Mymodel(models.Model):
    pass

for code in ['xxxx','yyyy','zzzz','qqqq','ssss','rrrr']:
    Mymodel.add_to_class('%s_count' % code, ForeignCountField(filter={'foreign_table__%s' % code: True})))
Alex Koshelev
Very good point, I forgot this approach (it's been so long since I've needed it, ever since Django got abstract model inheritance). This is probably a better solution than writing a metaclass.
Carl Meyer
Yeah, this's good working. Only one bad thing, that field's definition located in 2 different places in code: inside and outside class.
ramusus