def from_json(cls, json): try: return cls.objects.get(pk=json["id"]) except cls.DoesNotExist: keys = [f.name for f in cls._meta.get_fields()] subset = {key:json[key] for key in set(keys) & set(json.keys())} return cls.objects.create(**subset)