Hello,

I have got a model MobilePhone that has a producer, such as "alcatel",
a canonical name, such as "b-500 s" and a paraphrase type, such as
"a-341 i".  Each instance of MobilePhone is supposed to calculate all
its variants (such as alcatel b-500 s, alcatel b 500 s, alcatel b-500-
s, b-500 s, b 500 s, b-500-s, and so on). This worked perfectly as
long as the producer wasn't a field of its own, but part of the
canonical name. Since I introduced producer names (in order to
simplify the paraphrase types and to allow for variants of producer
names (cf. HP, Hewlett Packard and so on), the app became unusable:
Whenever one wants to see the list of all (461) mobile phones in the
admin interface, the development server becomes very slow and
eventually breaks down with a memory error. Below is the class
MobilePhone. What can I do?

######################################

class MobilePhone(models.Model):

    producer = models.ForeignKey(MobilePhoneProducer)
    canonical_name = models.CharField(max_length=200)
    paraphrase_type = models.CharField(max_length=50, choices=(
    ('n-127', 'n-127'),
    ('a-341 i', 'a-341 i'),
    ('neotouch', 'neotouch'),
    ))
    lexical_variants = models.ManyToManyField(LexicalVariant,
blank=True)
    n127_rx = re.compile('^(?P<chars>[a-z]+)\-(?P<numb>[0-9]+)$')
    a341_i_rx = re.compile('^(?P<chars1>[a-z]+)\-(?P<numb>[0-9]+) (?
P<chars2>[a-z])$')
    neotouch_rx = re.compile('^(?P<model>[a-z]+)$')

    def save(self, *args, **kwargs):
        self.canonical_name =
whitespace_normalize(self.canonical_name)
        super(MobilePhone, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.canonical_name

    def variants(self):
        self.save()
        paraphrases = self.paraphrase()
        for paraphrase in paraphrases:
            paraphrases.append(self.producer.name + ' ' + paraphrase)
        for variant in self.lexical_variants.all():
            paraphrases.append(variant)
            paraphrases.append(self.producer.name + ' ' + variant)
        if paraphrases:
            return ', '.join(paraphrases)
        else:
            return '<span style="color:red">(The paraphrase type doesn
\'t seem to match.)</span>'
    variants.allow_tags = True

    def the_lexical_variants(self):
        return ', '.join(self.lexical_variants.all())

    the_lexical_variants.short_description = "Lexical variants"

    def paraphrase(self):
        if self.paraphrase_type == 'n-127':
            test = self.n127_rx.search(self.canonical_name)
            if test:
                chars = test.group('chars')
                numb = test.group('numb')
                return [
                    chars + '-' + numb,
                    chars + numb,
                    chars + ' ' + numb,
                    ]
        elif self.paraphrase_type == 'a-341 i':
            test = self.a341_i_rx.search(self.canonical_name)
            if test:
                chars1 = test.group('chars1')
                chars2 = test.group('chars2')
                numb = test.group('numb')
                return [
                    chars1 + '-' + numb + ' ' + chars2,
                    chars1 + ' ' + numb + ' ' + chars2,
                    chars1 + numb + ' ' + chars2,
                    chars1 + '-' + numb + '-' + chars2,
                    chars1 + ' ' + numb + '-' + chars2,
                    chars1 + numb + '-' + chars2,
                    chars1 + '-' + numb + chars2,
                    chars1 + ' ' + numb + chars2,
                    chars1 + numb + chars2,
                    ]
        elif self.paraphrase_type == 'neotouch':
            test = self.neotouch_rx.search(self.canonical_name)
            if test:
                gmodel = test.group('model')
                return [
                    gmodel,
                    ]
        return []

###############################

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

Reply via email to