Yupeek/django-rest-models

Make contentType optional in sql insert and update.

RabidCicada opened this issue · 2 comments

Make contentType optional in SQLInsertCompiler and SQLUpdateCompiler. This allows a regular django.core.files.base.File() object to be fed to django-rest-models. The django-rest-models setup currently just expects the content_type to exist and blindly references it. My particular use case is a django-admin command on the django-rest-models consumer app to allow import of files into the system. The general usecase is to let anyone simply use a django File() object to feed a django-rest-model.

def _import_stuff(self, tmpdir):
      gen_exp = (d for d in tmpdir.iterdir() if d.is_dir())
      for d in gen_exp:
          with open(str(d / 'data.json'), 'r') as f:
              deserializer = serializers.deserialize("json", f.read())
              for deserialized_stuff in deserializer:
                  newstuff = deserialized_stuff.object

                  for field in newstuff._meta.fields: # pylint: disable=protected-access
                      if isinstance(field, models.FileField):
                          field = getattr(newstuff, field.attname)
                          if field:
                              thefile = File(open(str(d / str(field)), 'rb'))
                              field.save(str(field), thefile, save=False)

                  #Save entire model
                  newstuff.save(force_insert=True)

I've tested defaulting to None and it works. I've looked around and we could also default to 'application/octet-stream' though I have not tested that.

Thank you for your time 👍

Don't hesitate to request us if you want to add the DEFAULT_CONTENT_TYPE setting