myyang / django-pb-model

Protobuf mixin for django model
Other
112 stars 22 forks source link

Proxy Models #29

Closed nakamurabh closed 3 years ago

nakamurabh commented 3 years ago

Hi there,

Is there a way to use Proxy model? Im getting this error when using the proxy on a proto model

class Vehicle(ProtoBufMixin):
    # code here

class Car(Vehicle):
    class Meta:
        proxy = True

error on makemigrations


SystemCheckError: System check identified some issues:

ERRORS:
?: (models.E017) Proxy model 'Car' contains model fields.
fleet.Car.account_id: (models.E006) The field 'account_id' clashes with the field 'account_id' from model 'fleet.vehicle'.
myyang commented 3 years ago

Hi @nakamurabh,

could you provide more information about Python, Django version, and sample code to reproduce the error? It seems not the issue with the proxy model, I didn't encounter exceptions and migrated successfully with Python3.8, Django3.1, and the following code:

import json

from django.db import models

from pb_model import fields
from pb_model.models import ProtoBufMixin

from . import models_pb2

def json_serializer(pb_obj, pb_field, dj_value):
    setattr(pb_obj, pb_field.name, '_____' + json.dumps(dj_value) + '_____')

def json_deserializer(instance, dj_field_name, pb_field, pb_value):
    setattr(instance, dj_field_name, json.loads(pb_value)[5:-5])

class Issue21(ProtoBufMixin):
    pb_model = models_pb2.Issue21

    pb_2_dj_field_serializers = {
        fields.JSONField: (json_serializer, json_deserializer),
    }

    dt_field = models.DateTimeField(auto_now=True)
    cus_field = fields.JSONField(null=True)

class Issue29(Issue21):
    class Meta:
        proxy = True
nakamurabh commented 3 years ago

Hi,

Specs: Django 2.2.17 Python 3.7.4

Protobuff

message Car {
    int64 car_id = 1;
    string license_plate = 2;
}

Base class

from models.proto import  Car as CarProto

class Car(ProtoBufMixin):
    pb_model = CarProto
    pb_2_dj_fields = '__all__'

    def __str__(self):
        return (
            f'pk: {self.pk} - '
            f'id: {self.car_id} - '
            f'license_plate: {self.license_plate}'
        )

Migration

# migration
# Generated by Django 2.2.17 on 2021-03-23 21:46

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('users', '0001_car'),
    ]

    operations = [
        migrations.CreateModel(
            name='Car',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('car_id', models.BigIntegerField(null=True)),
                ('license_plate', models.TextField(null=True)),
            ],
            options={
                'abstract': False,
            },
        )
    ]

Proxy Model

class Van(Car):
    class Meta:
        proxy = True
python manage.py makemigrations 
SystemCheckError: System check identified some issues:

ERRORS:
?: (models.E017) Proxy model 'Van' contains model fields.
model.Van.car_id: (models.E006) The field 'car_id' clashes with the field 'car_id' from model 'model.car'.
model.Van.license_plate: (models.E006) The field 'license_plate' clashes with the field 'license_plate' from model 'model.car'.

I also tried updating the Django to 3.1.7 but without success

myyang commented 3 years ago

@nakamurabh would you like to try and review #30 ?