Commits

Javed Khan committed 892964f

added related objects

  • Participants
  • Parent commits 0da280b

Comments (0)

Files changed (3)

File schema/models.py

 
 class Objekt(models.Model):
     model = models.ForeignKey(Model, related_name="instances")
-    parent = models.ForeignKey('Objekt', null=True, blank=True)
+    parent = models.ForeignKey('Objekt', null=True, blank=True, related_name="related")
 
     def __unicode__(self):
-        return "instance of %s" % (self.model.name)
+        if self.attrs.count():
+            return '-'.join(list(self.attrs.values_list('value', flat=True)))
+        else:
+            return "instance of %s" % (self.model.name)
 
 
 class KeyValue(models.Model):

File schema/templates/schema/object_list.html

             <td><strong>{{ attr.key }}</strong></td>
             <td>{{ attr.value }}</td>
             {% endfor %}
+            {% if object.related.count %}
+            <td>
+                <strong>Related {{ object.related.all.0.model }}</strong>
+            </td>
+            <td>
+                <ol>
+                    {% for rel in object.related.all %}
+                    <li>{{ rel }}</li>
+                    {% endfor %}
+                </ol>
+            </td>
+            {% endif %}
         </tr>
         {% endfor %}
     </table>

File schema/views.py

 from django.views.generic.list import ListView
 from django.core.urlresolvers import reverse_lazy
 from django.shortcuts import get_object_or_404
+from django.utils.datastructures import SortedDict
 from django.template.defaultfilters import slugify
 from django.contrib import messages
 from django.forms import BaseForm
 
     def get_form_class(self):
         model = get_object_or_404(Model, slug=self.kwargs['slug'])
-        fields = {}
+        fields = SortedDict()
         for field in model.fields.all():
             if field.kind == 'DROPDOWN':
                 fields[field.name] = forms.ChoiceField(choices=(
                     (x, x) for x in field.choices.split(',')))
             else:
                 fields[field.name] = FIELD_WIDGETS[field.kind]
+        if model.child_id:
+            fields[model.child.name] = forms.ModelChoiceField(
+                    queryset=Objekt.objects.filter(model=model.child))
         BaseForm.helper = FormHelper()
         BaseForm.helper.form_tag = False
         return type('ObjectForm', (BaseForm,), { 'base_fields':
         model = get_object_or_404(Model, slug=self.kwargs['slug'])
         objekt = Objekt.objects.create(model=model)
         kwargs = form.cleaned_data.copy()
+        if model.child_id:
+            objekt.parent = kwargs.pop(model.child.name)
+            objekt.save()
         for k, v in kwargs.iteritems():
             k = slugify(k)
             attr = KeyValue.objects.create(key=k, value=v,