Question

in Django, I have a model 'Event' which has a field called 'messplatz' (DB-Id) which references to another table located on a different database. Since Djangos foreignKey-field is not possible with multiple databases, the field 'messplatz' is an Integerfield, storing the PK-Value of the referenced 'Messplatz':

class Event(models.Model):
    messplatz = models.Integerfield()

The problem I have is the representation of model Event with django-tables 2:

The model 'Messplatz' is defined in the 'prodis' app, which routes to a different database. My view looks as follows:

def eventList(request):
    table = EventTable(Event.objects.all())
    RequestConfig(request).configure(table)
    return render(request, 'mkv/eventList.html', {'table': table})

In the view I want the field 'Messplatz' to display the name of the actual Messplatz-Object (1). Moreover, I want it to be a link to another view (2). I can achive (1) by defining a custom render-method for the field 'messplatz' in tables.py:

from prodis.models import Messplatz

class EventTable(tables.Table):

    class Meta:
        model = Event

def render_messplatz(self):
    return '%s' % Messplatz.objects.get(id=self.messplatz) 

But for (2) I have no clue. If I change the custom render method to

def render_messplatz(self):
    return '<a href="...">%s<a>' % Messplatz.objects.get(id=self.messplatz) 

the link-tags are displayed as plain-text by django-tables 2. When I use the LinkColumn of django-tables, I cannot achieve (1) either, because you can't use LinkColumn and custom_render method simultaneously.

Does anybody has an idea? Thanks in advance

Was it helpful?

Solution

Got it working somehow. However, it's a very bad solution and I would appreciate every other idea.

I used django-tables2 Template Column:

class EventTable(tables.Table):
    #override messplatz-column
    messplatz = tables.TemplateColumn('{% load url from future %}<a href="{% url \'messplatz_events\' record.messplatz %}">{{ record.get_messplatz }}</a>')
    #Hint: {% load url from future %} is only needed for django versions < 1.5

    class Meta:
        model = Event

To access record.get_messplatz, I had to move the get_messplatz method to the Event-Model:

from prodis.models import Messplatz

class Event(models.Model):
    messplatz = models.IntegerField()

    def get_messplatz(self):
        return Messplatz.objects.get(mpl_id=self.messplatz)

Won't mark this as solution, because I think it's a very bad one. But I hope this answer helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top