Fixed some bugs in many to many and generic properties
diff --git a/planetstack/apigen/api.template.py b/planetstack/apigen/api.template.py
index 81a1eca..690bb13 100644
--- a/planetstack/apigen/api.template.py
+++ b/planetstack/apigen/api.template.py
@@ -35,7 +35,11 @@
class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer):
id = serializers.Field()
{% for ref in object.refs %}
- {{ ref.plural }} = serializers.HyperlinkedRelatedField(view_name='{{ ref }}-detail')
+ {% if ref.multi %}
+ {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail')
+ {% else %}
+ {{ ref }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail')
+ {% endif %}
{% endfor %}
class Meta:
model = {{ object }}
@@ -53,11 +57,11 @@
{% for object in generator.all %}
class {{ object.camel }}List(generics.ListCreateAPIView):
- queryset = {{ object.camel }}.objects.all()
+ queryset = {{ object.camel }}.objects.select_related.all()
serializer_class = {{ object.camel }}Serializer
class {{ object.camel }}Detail(generics.RetrieveUpdateDestroyAPIView):
- queryset = {{ object.camel }}.objects.all()
+ queryset = {{ object.camel }}.objects.select_related.all()
serializer_class = {{ object.camel }}Serializer
{% endfor %}
diff --git a/planetstack/apigen/modelgen b/planetstack/apigen/modelgen
index 4c8a0f1..d2e80f5 100755
--- a/planetstack/apigen/modelgen
+++ b/planetstack/apigen/modelgen
@@ -11,9 +11,17 @@
sys.path.append('.')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
-from django.db.models.fields.related import ForeignKey
+from django.db.models.fields.related import ForeignKey, ManyToManyField
from core.models import *
+def singular(foo, keys):
+ for k in keys:
+ if (foo==k+'es'):
+ return k
+ elif (foo==k+'s'):
+ return k
+ raise Exception('Plural to singular error for %s'%foo)
+
g = globals()
def enum_classes():
@@ -66,6 +74,18 @@
obj.refs.append(refobj)
else:
obj.props.append(f.name)
+ m2m = obj.model._meta.many_to_many
+ for f in m2m:
+ try:
+ related_model_name = f.m2m_reverse_field_name()
+ except:
+ related_model_name = f.m2m_db_table().rsplit('_',1)[-1]
+
+ if related_model_name in self.keys():
+ refobj = self[related_model_name]
+ obj.multi=True
+ refobj.refs.append(obj)
+
def main():
try: