Hi all

I am quite new to phyton/django, so please be patient :)

I've been put in charge to maintain a django package that has been
developed for our company last year. I already fixed some bugs/issues,
but there is a bug that goes beyond my limited knowledge of the
framework. I already tried to get some help on irc channel, and I was
suggested to ask for help here.

The situation is this: the site and the data behind it can be managed
both from the django built-in administrative web backend and from a
set of apis, that are called via http passing the parameters in json
in the request body.

Unfortunately there are some properties that are properly managed and
set through the admin backend, but not from the APIs, ad I can't
figure out how this can be done as the properties themselves are
defined bypassing standard models class objects declaration, with a
direct mysql call issued with cursor.execute.

Here I paste the relevant (I hope) parts of code:

#---------------------------------------------
#Models.py:
#---------------------------------------------


class Article(models.Model):
    name = models.CharField(max_length=255)
    status = models.CharField(max_length=1, choices=STATUS,
default='S', blank=True)
    modified = models.DateTimeField(null=True, blank=True,
editable=False)

    @property
    def labels(self):
        cursor = connection.cursor()
        cursor.execute("""
            select
                t.name, t.status, s.id, s.label
            from
                articles_labels_tab ft
            inner join
                article_label t on t.name=ft.label_id
            left join
                article_color s on s.id=ft.colors
            where
                ft.article_id=%s
        """, (self.pk,))
        fields = ('name', 'status', 'id', 'label')
        return [dict(zip(fields, row)) for row in cursor.fetchall()]


#---------------------------------------------
#Urls.py
#-------------------------
from django.conf.urls.defaults import *

urlpatterns = patterns('',
   (r'^(addarticle/(?:(?P<pk>[0-9]+)/)?', 'add_article'),
)


#---------------------------------------------
#Views.py
#---------------------------------------
from django.newforms import ModelForm

from articles.models import Article, Label

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        exclude = ('labels',) #if I comment this line when the class
is instantiated an error is raised


def add_article(request, api_key, pk=None):
        instance = None
        data = dict()
    try:
        data.update(cjson.decode(request.raw_post_data))
    except cjson.DecodeError, e:
        return HttpResponseBadRequest("Error decoding json data: %s" %
e)
    except TypeError:
        return HttpResponseBadRequest("Decoded article data is not a
dictionary.")
    if 'labels' in data:
        _labels = data['labels']
        del(data['labels'])
    else:
        _labels = list()
    form = ArticleForm(data, instance=instance)
    if not form.is_valid():
        return HttpResponseBadRequest("Missing or incorrect data: %s."
% form.errors.as_text().replace('\n', ''))
    try:
        article = form.save()
        labels = list()
        for name in _labels:
                shortname=shorten(name)
            try:
                label = Label.objects.get(shortname=shortname)
            except ObjectDoesNotExist:
                label = Label(shortname=shortname, name=name)
                label.save()
            labels.append(label)
        article.labels = labels  #Here an error is raised, if I
comment this line everything works fine, but the property isn't,
obviously, set...
    except IntegrityError, e:
        return HttpResponseBadRequest("Article %s is already present."
% e)

#---------------------------------------------
#---------------------------------------------

The labels property of article can be set through the admin interface,
but not from the api call...
My question is: how can I have the property set through the APIs?

Any help will be appreciated, thanks for your time :)

Space

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to