Search code examples
pythondjangodjango-viewsdjango-extensions

UUIDField from django-extensions is not available in Class Based Views


I have a web application where I use a couple of UUIDFields. In the Admin UI, in function based views and other Python code, these fields work as expected. However, when trying to list them in the 'fields' in a CBV, I get the error:

FieldError(message) django.core.exceptions.FieldError: Unknown field(s) (ct_id) 

The subject field here is ct_id. But another one does the same.

From models.py:

ct_id = UUIDField(_("UUID"), version=4, help_text=_('A unique identifier for this PCT.'))

As mentioned above, they work in Admin lists:

    list_display = ('data_name','prj_name','published','ct_id')
admin.site.register(DvBoolean, DvBooleanAdmin)

In function based views to create JSON for DynaTree:

pct_json['tooltip'] = 'ct-'+pct.ct_id + " : " +pct.description

But in a CBV, this raises the error:

fields =['published','prj_name','data_name','ct_id',]

Any ideas on how to make this work? I only want to render them for display, not for editing.

Thanks


Solution

  • I am not sure if this is the only or best way to solve the problem. But, since I needed to use get_context_data() in the view anyway. I am now doing this to get ct_id into the view template:

    def get_context_data(self,**kwargs):
        context = super(DvStringUpdateView, self).get_context_data(**kwargs)
        semlinks = []
        obj = get_object_or_404(DvString,pk=context['object'].id)
        if obj.resource_uri:
            urilist = obj.resource_uri.splitlines()
            attrlist = obj.sem_attr.splitlines()        
            for n in range(0,len(urilist)):
                semlinks.append(attrlist[n] + ' = ' + unquote(urilist[n]))
    
        context['semlinks'] = semlinks
        context['ct_id'] = obj.ct_id
        return context
    

    IS there a bug in the way the UUIDField is defined causing it to not be available in CBVs? This is at least a workaround.