﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
806	module methods and adding a new record	Ian@…	Adrian Holovaty	"Hi.
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):
        return self.name;
    
           
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 t.id as id, t.name as name,  t.description as description, t.type_id as type_id
            FROM conf_tags t, conf_tagtypes tt
            WHERE t.type_id =tt.id 
            AND t.name = %s 
            AND tt.name = %s """""", [tag_real_name, tag_type] )
        row = cursor.fetchone()
        if row:
            return Tag(*row)
        else:
            if insert_if_not_there:
                t = tagtypes.get_object(name__exact = tag_type )
                newtag = t.add_tag( tag_real_name )
                return newtag
            else:
                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/conf.py"", 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
normal

<class 'django.models.conf.TagType'>
>>> t.add_tag('foobar')
foobar
>>> x =t.add_tag('foobar2')
>>> x.id
55L
>>>
}}}

so I am at a loss ;("	defect	closed	Core (Other)		normal	fixed			Unreviewed	0	0	0	0	0	0
