Render onetoone relationship in django - django

I have 2 models:
class ModbusDevice(models.Model):
ixModbusDevice = models.AutoField(primary_key=True)
sModbusName = models.CharField(verbose_name='Device Name',max_length=100)
iPort = models.IntegerField(verbose_name='Port')
iSlave = models.IntegerField(verbose_name='Slave ID')
sIP = models.GenericIPAddressField(verbose_name='IP Address')
class Meta:
db_table = 'TModbusDevice'
class BACnetDevice(models.Model):
ixBACnetDevice = models.AutoField(primary_key=True)
ixModbusDevice = models.OneToOneField(ModbusDevice)
sIP = models.GenericIPAddressField(verbose_name='IP Address')
iPort = models.IntegerField(verbose_name='Port')
iSubnet = models.IntegerField(verbose_name='Subnet', blank=True)
bStatus = models.BooleanField(default=False)
class Meta:
db_table = 'TBACnetDevice'
As you can see each BACnetDevice is related to each ModbusDevice via one to one. What I'm trying to do is display a table with mixed fields from each model.
I want to display the ModbusDevice name along with sIP, iPort and iSubnet from my BACnetDevice model like this(somewhat what i had working):
I'm having difficulty with rendering the model's formset foreign key field
I've tried this so far in my view:
BACnetFormSet = inlineformset_factory(ModbusDevice, BACnetDevice, fields=('sIP','iPort','iSubnet',))
bacnet_forms = BACnetFormSet()
return render(
request,
'app/create_bacnet.html',
context_instance = RequestContext(request,
{
'title':'Create BACnet Device',
'tag': 'create_bacnet',
'bacnet_forms': bacnet_forms
})
)

Related

nested objects in Django rest framework

I want to design solution for ordering items. I have endpoint that create orders BUT I need to to have items object in the order. let me show you the code
class ItemModel(models.Model):
name = models.CharField(max_length=50)
price = models.FloatField()
discretion = models.CharField(max_length=500)
available = models.BooleanField(default=True)
class OrderModel(models.Model):
phone = models.CharField(max_length=20)
delevary_time = models.DateTimeField()
class CartModel(models.Model):
order = models.ForeignKey(OrderModel, on_delete=models.CASCADE, related_name='order_m')
item = models.ForeignKey(ItemModel, on_delete=models.CASCADE, related_name='item_m')
I need endpoint that create order to me. her what I did
class CartSerializer(serializers.ModelSerializer):
class Meta:
model = CartModel
exclude = ['order',]
depth = 2
class OrderSerializer(serializers.ModelSerializer):
cart = serializers.SerializerMethodField()
class Meta:
model = OrderModel
fields = ['phone', 'state', 'delevary_time', 'cart']
def get_cart(self, obj):
cart = CartModel.objects.filter(order__id=obj.id)
serializer = CartSerializer(cart, many=True)
return serializer.data
this is the endpoint
router.register('order', OrderViewSet, 'api-order')
{
"phone": 124997988698,
"delevary_time": "2020-07-17T19:34:00",
"cart": [
{
"item": 1
},
{
"item": 2
}
]
}
when I post the json it don't save the cart it only save the oder phone and delevary_time. How I can save the cart at the same time
class CartSerializer(serializers.ModelSerializer):
class Meta:
model = CartModel
exclude = ['order',]
depth = 2
class OrderSerializer(serializers.ModelSerializer):
order_m = CartSerializer(many=True) # adding this
class Meta:
model = OrderModel
fields = ['phone', 'state', 'delevary_time', 'order_m']
def create(self, validated_data):
cart_data = validated_data.pop('order_m')
order = OrderModel.objects.create(**validated_data)
for c in cart_data:
CartModel.objects.create(order=order, **c)
return order

How to do a relationship using DRF to another table without foreignKey

I can not do relatioships between two tables without relationships.
My models are :
class exampleModel(models.Model):
quantity = models.IntegerField(blank=False, null=True)
comment = models.CharField(max_length=100 , blank=True, null=True)
class Meta:
db_table = "example"
class Logger(models.Model):
id_table = models.IntegerField()
table = models.CharField(max_length=20 , blank=True, null=True)
comment = models.CharField(max_length=100 , blank=True, null=True)
action = models.CharField(max_length=100 , blank=True, null=True)
date_created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "logger"
I already have filled logger Model, but , I have not be able made the exampleSerializer.
My serializers are:
class LoggerSerializer(serializers.ModelSerializer):
class Meta:
db_table = u'logger'
model = Logger
fields = '__all__'
class exampleSerializer(serializers.ModelSerializer):
last_log = LoggerSerializer(read_only=True)
class Meta:
db_table = 'example'
model = ExampleModel
fields = ( 'id' , 'last_log' , 'quantity')
in logger saves :
id_table : 'primary key of example',
table : 'example'
comment : 'custom comment',
action : "CRUD"
You can query last logger in view, then pass it to serializer in context.
Then do something like:
class exampleSerializer(serializers.ModelSerializer):
last_log = serializers.SerializerMethodField()
class Meta:
db_table = 'example'
model = ExampleModel
fields = ('id', 'last_log', 'quantity')
def get_last_log(self, obj):
last_log = obj.state(self.context['last_log'])
last_log_serializer = LoggerSerializer(last_log)
return last_log_serializer.data
You pass to context, with something like:
exampleSerializer(queryset, context ={'last_log': last_log_object})
Also You can do the query in exampleSerializer:
class exampleSerializer(serializers.ModelSerializer):
last_log = serializers.SerializerMethodField()
class Meta:
db_table = 'example'
model = ExampleModel
fields = ('id', 'last_log', 'quantity')
def get_last_log(self, obj):
logger_queryset =Logger.objects.filter(table = self.Meta.db_table, id_table = obj.id)
return LoggerSerializer(logger_queryset).data

How to serialize an object instance which gets the data from 2 separate models using Django Rest Framework?

Query :
I have a GET request which gives the data from the 2 models (single_tracklog_object in View). However, when I serialize this object instance using the serializers I am getting an empty data for devices as below.
{
"lat": "51.0038",
"lon": "8.0038",
"speed": 50,
"course": 5,
"device": {} # this needs to be having a value but is empty.
}
I am not understanding why its happening. Please guide me on this.
My Models :
class Tracklogs(models.Model):
tracklog_id = models.AutoField(primary_key=True)
device = models.ForeignKey(Tracking_devices, related_name='tracklogs')
lat = models.CharField(max_length=10, null=False)
lon = models.CharField(max_length=11, null=False)
.........
#timestamps
tracker_datetime = models.DateTimeField(blank=True, null=True)
class Meta:
get_latest_by = "tracker_datetime"
class Tracking_devices(models.Model):
device_id = models.AutoField(primary_key=True)
device_type = models.ForeignKey(Device_type)
user = models.ForeignKey(User, null=False)
name = models.CharField(max_length=100)
description = models.CharField(max_length=256, blank=True, null=True)
My View :
serializer_class = ShowLocationInfoSerializer
def get(self, request, *args, **kwargs):
# get the imei from the url
imei = self.kwargs['imei']
try:
single_tracklog_object = Tracklogs.objects.filter(device__imei = imei).values('lat', 'lon','speed','course','device','device__name').latest()
# Here its causing problem!!!
serializer = self.serializer_class(single_tracklog_object)
return Response(serializer.data)
except ObjectDoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
My Serializer:
class Tracking_DeviceSerializer(serializers.ModelSerializer):
name = serializers.CharField(read_only=True)
class Meta:
model = Tracking_devices
fields = ('name')
class ShowLocationInfoSerializer(serializers.ModelSerializer):
lat = serializers.CharField(read_only=True)
lon = serializers.CharField(read_only=True)
speed = serializers.IntegerField(read_only=True)
course = serializers.FloatField(read_only=True)
device = Tracking_DeviceSerializer()
class Meta:
model = Tracklogs
fields = ('lat', 'lon', 'tracker_datetime', 'speed', 'course', 'device' )
Thanks in advance.

Django model formset displaying db data instead of empty formset

I'm using a model formset to allow a user to dynamically add duplicate forms to a table(modbus registers since a device can have multiple registers).
The problem I'm having is that as soon as I save the formset(store to db), when I come back to the same page to create a new device with registers, django renders all the previously created forms instead only 1 empty form(then being able to add forms to that).
Here is an example after having created 3 forms in my formset(stored in db):
Here is what it should look like:
It should only display 1 empty form in my formset, the user can then dynamically add forms.
my view:
def create_modbus_view(request):
modbus_device = CreateModbusForm()
RegisterFormset = modelformset_factory(Register, form=ModbusRegistersForm)
register_forms = RegisterFormset()
return render(
request,
'app/create_modbus.html',
context_instance = RequestContext(request,
{
'title':'Create Modbus Device',
'tag': 'create_modbus',
'modbus_device': modbus_device,
'register_forms': register_forms
})
)
models:
class Register(models.Model):
ixRegister = models.AutoField(primary_key=True)
sRegisterName = models.CharField(max_length=100)
iStartingAddr = models.IntegerField()
bRange = models.BooleanField(default=False)
ixRegisterType = models.ForeignKey(RegisterType)
iOffset = models.IntegerField(blank=True, null=True)
class Meta:
db_table = 'TRegister'
class ModbusDevice(models.Model):
ixModbusDevice = models.AutoField(primary_key=True)
sModbusName = models.CharField(verbose_name='Device Name',max_length=100)
iPort = models.IntegerField(verbose_name='Port')
iSlave = models.IntegerField(verbose_name='Slave ID')
sIP = models.GenericIPAddressField(verbose_name='IP Address')
class Meta:
db_table = 'TModbusDevice'
form:
class ModbusRegistersForm(ModelForm):
ixRegisterType = ModelChoiceField(queryset=RegisterType.objects.all()),
bRange = BooleanField(required=False)
bRange.widget.attrs['data-form'] = 0
class Meta:
model = Register
fields = ['sRegisterName','iStartingAddr','bRange','ixRegisterType','iOffset']
widgets = {
'sRegisterName': TextInput(attrs={'placeholder': 'Register Name','class': 'form-control', 'data-form': '0'}),
'iStartingAddr': TextInput(attrs={'placeholder': 'Starting address','class': 'form-control','data-form': '0'}),
'iOffset': TextInput(attrs={'placeholder': 'Address offset','class': 'form-control','data-form': '0'}),
}
class CreateModbusForm(ModelForm):
class Meta:
model = ModbusDevice
fields = ['sModbusName','iPort', 'iSlave', 'sIP']
widgets = {
'sModbusName': TextInput(attrs={'placeholder': 'Device Name','class': 'form-control','id': 'device-name',}),
'iPort': TextInput(attrs={'placeholder': 'Port','class': 'form-control','id': 'port',}),
'iSlave': TextInput(attrs={'placeholder': 'Slave id','class': 'form-control','id': 'slave-id',}),
'sIP': TextInput(attrs={'placeholder': 'Modbus IP address','class': 'form-control','id': 'ip-address',})
}
this seems to work
django modelformset_factory sustains the previously submitted data even after successfully created the objects
seems i just have to prevent it from querying my tables

Serialize related objects in django rest framework

How can I serialize related objects in DRF. Am having three models auth_user (User), ModelA, ModelB.
My models:
class ModelA(models.Model):
user = models.OneToOneField(auth_user, related_name = 'modelA')
name = models.CharField(max_length = 30)
class ModelB(models.Model):
owner = models.OneToOneField(auth_user)
user = models.OneToOneField(auth_user, related_name = 'modelB')
type = models.ForeignKey(ModelD)
cost = models.IntegerField()
class ModelD(models.Model):
type_desc = models.CharField(max_length = 40)
My serialilzers:
class A(serializers.ModelSerializer):
class Meta:
model = ModelA
fields = ('name', )
class B(serializers.ModelSerializer):
class Meta:
model = ModelB
fields = ('type', 'cost', )
class AuthUserSerilaizer(serializers.ModelSerializer):
userA = serializers.RelatedField(source = 'modelA')
userB = serializers.RelatedField(source = 'modelB')
class Meta:
model = User
fields = ('email', 'password', 'userA', 'userB', )
write_only_fields = ('password',)
#transaction.commit_manually
def restore_object(self, attrs, instance = None):
try:
user = User.objects.create_user(email = attrs.get('email'), password = attrs.get('password'))
modela = ModelA(user =user, name = attrs.get('name'))
modela.save()
transaction.commit()
return User(email = attrs.get('email'))
except Exception ,e:
transaction.rollback()
print repr(e)
JSON I'm passing
data = {'email':'123#gmail.com,
'password' : 'dummy',
'userA' : {'name' :'123'},
'userB':{'type':1,'cost':'100'}
Whenever am making a POST request the 'attrs' gets only email and password not userA and userB, why? How can I deserialize and create data serially in the respective tables.
try this in your view
from django.core.serializers.json import DjangoJSONEncoder
import json
response = json.dumps(data,cls=DjangoJSONEncoder)
return HttpResponse(response, mimetype="application/json")
and in success function you can access using
usera_name = response.userA.name
userb_type = response.userB.type
userb_cost = response.userB.cost