Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#806 closed defect (fixed)

module methods and adding a new record

Reported by: Ian@… Owned by: adrian
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I'm having some 'fun' with using module functions, and would appreciate some enlightenment.

this is the class & function.

class TagType(meta.Model):
    name = meta.CharField(maxlength=40, core=True, unique=True)
    class META:
        admin = meta.Admin();
        ordering = [ 'name' ]

    def __repr__(self):
class Tag(meta.Model):
    name = meta.CharField(maxlength=40, core=True )
    description = meta.TextField( unique=False, blank=True)
    type = meta.ForeignKey(TagType, radio_admin=True, default=1)
    class META:
        admin = meta.Admin()
        ordering = [ 'name' ]
        unique_together = (("name", "type"),)
    def _module_name_to_id(tagname, insert_if_not_there=False):
        from keyman.apps.conf.models.conf import tagtypes, TagType
        "convert a XXXX:TAG into it's ID name"
        format = tagname.lower().split(":")
        tag_real_name = format.pop()
        tag_type = "normal"
        if ( format.__len__() > 0 ):
            tag_type =  format[0]

        cursor= db.cursor()
        cursor.execute( """
            SELECT as id, as name,  t.description as description, t.type_id as type_id
            FROM conf_tags t, conf_tagtypes tt
            WHERE t.type_id 
            AND = %s 
            AND = %s """, [tag_real_name, tag_type] )
        row = cursor.fetchone()
        if row:
            return Tag(*row)
            if insert_if_not_there:
                t = tagtypes.get_object(name__exact = tag_type )
                newtag = t.add_tag( tag_real_name )
                return newtag
                raise TagDoesNotExist

so what it tries to do is return a id when given a the type/name combo like

a = tags.get_name_to_id("site:foo")

and that works pretty well, as I'm going straight to the DB to get that.
but when I try to add a new tag I am using the model API.

>>> from django.models.conf import *
>>> a=tags.name_to_id("foobar",True)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.4/site-packages/keyman/apps/conf/models/", line 64, in _module_name_to_id
    newtag = t.add_tag( tag_real_name )
AttributeError: 'TagType' object has no attribute 'add_tag'

when I do the same code from the python command line I see

>>> t = tagtypes.get_object(name__exact='normal')
>>> t

<class 'django.models.conf.TagType'>
>>> t.add_tag('foobar')
>>> x =t.add_tag('foobar2')

so I am at a loss ;(

Change History (1)

comment:1 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

This should no longer be an issue with magic-removal.

Note: See TracTickets for help on using tickets.
Back to Top