shymonk/django-datatable

unable to use foreign key

keiser1080 opened this issue · 4 comments

Hi,
i am trying to use django-datatable with a model containing foreign key
but i get the error :

 (1054, "Unknown column 'event.name' in 'order clause'")
File "/django_datatable-0.1.2-py2.7.egg/table/views.py" in convert_queryset_to_values_list
  113.             for obj in queryset

My model:

class Notification(models.Model):
    user = models.ForeignKey(User)
    event = models.ForeignKey(Event)
    category = models.ForeignKey(Category)
    aknowledge = models.BooleanField(default=False)
    read = models.BooleanField(default=False)
    creation_date = models.DateTimeField(auto_now=True, blank=True)
    def __unicode__(self):
        return u'%s_%s_%s' % (self.id, self.user, self.event)
    def get_absolute_url(self):
        return reverse('server_edit', kwargs={'pk': self.pk})

my view:

def notification(request):
     notification = NotificationTable()
     return render(request, "notification.html", {'notification': notification})

class NotificationAPI(FeedDataView):
    token = NotificationTable.token
    def get_queryset(self):
        return super(NotificationAPI, self).get_queryset().filter(read=False)

My table:

class NotificationTable(Table):
    id = Column(field='id', header=u'id')
    user = Column(field='user.username', header=u'user')
    event = Column(field='event.name', header=u'event')
    category = Column(field='category.name', header=u'category')
    aknowledge = CheckGlyphicon(field='aknowledge', header=u'aknowledge')
    read = CheckGlyphicon(field='read', header=u'read')
    # creation_date = DatetimeColumn(field='creation_date', header=u'creationdate', format='%H:%M:%S %d-%m-%Y', searchable=False)
    class Meta:
        model = Notification
        info = True
        info_format = u"Total: _TOTAL_  First: _START_  Last: _END_"
        zero_records = u'No records found'
        ajax = True
        ajax_source = reverse_lazy('notification_api')
        attrs = {'class': 'table-striped'}
        # sort = [(4, 'asc'), (5, 'desc')]
        # ext_button = True

what's wrong ?

Great issue.
It's a bug about sorting field across different relations. I have fixed in HEAD revision of master branch.

thanks for the feed back,

so i need to clone again .
clone https://github.com/shymonk/django-datatable.git right ?

Yes,please let me know if it works.

yeees it's working!

Could you add on the wiki or documentation the usage with a Fk field,
that work for my but i think it's not clear for the others :)

But i see still a small issue with sorting, using the same datable.py and model.py i can not sort using id column, when i click on id only the category change the other column stay unchanged.