Commits

Javed Khan  committed 234161e

save object

  • Participants
  • Parent commits 97f2ccd

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)
+
+    def __unicode__(self):
+        return "instance of %s" % (self.model.name)
+
+
+class KeyValue(models.Model):
     key = models.CharField(max_length=100)
     value = models.TextField()
+    objekt = models.ForeignKey(Objekt, related_name="attrs")
 
     def __unicode__(self):
-        return "instance of %s" % (self.model.name)
+        return self.key

File schema/templates/base.html

     </div>
     <!-- end navbar -->
 
+
+    <div id="messages">
+        {% if messages %}
+        {% for message in messages %}
+        <div class="alert alert-{{message.tags}}">
+            <a class="close" data-dismiss="alert">×</a>
+            {{message}}
+        </div>
+        {% endfor %}
+        {% endif %}
+    </div>
+
     <div class="container">
     {% block content %}
     {% endblock %}

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.template.defaultfilters import slugify
+from django.contrib import messages
 from django.forms import BaseForm
 
 from crispy_forms.helper import FormHelper
 
 from schema.forms import ModelForm, FieldFormset
-from schema.models import Model
+from schema.models import Model, Objekt, KeyValue
 from schema.mixins import InlineFormsetMixin
 from schema.widgets import FIELD_WIDGETS
 
             fields
         })
 
+    def form_valid(self, form):
+        model = get_object_or_404(Model, slug=self.kwargs['slug'])
+        objekt = Objekt.objects.create(model=model)
+        kwargs = form.cleaned_data.copy()
+        for k, v in kwargs.iteritems():
+            k = slugify(v)
+            attr = KeyValue.objects.create(key=k, value=v,
+                    objekt=objekt)
+        messages.success(self.request, 'New %s created' % (model.name))
+        return super(ObjectCreateView, self).form_valid(form)
+
 
 home = HomepageView.as_view()
 model_create = ModelCreateView.as_view()