我正在从扫描仪将扫描后的序列号发送到Django服务器的发布请求。
{“ serial_number”:“ 70:B3:D5:A9:C8:62”,“ bom”:“ 2019/05/08”}
views.py
def camera_register(request):
form = forms.RegisterCamerasForm(data=request.POST)
if form.is_valid():
instances = form.save(commit=False)
# instances.serial_number = instances.serial_number.lower()
instances.last_sighting_ip = request.META.get('REMOTE_ADDR')
instances.last_sighting_time = datetime.datetime.now()
form.save()
form.save_m2m()
return JsonResponse(1, safe=False)
else:
return JsonResponse(form.errors, status=200)
forms.py
class RegisterCamerasForm(forms.ModelForm):
def clean(self):
super().clean()
class Meta:
fields = '__all__'
exclude = ['group']
model = camera_models.Camera
models.py
class Camera(TimeStampedModel):
public_identifier = models.UUIDField(unique=True,
default=uuid.uuid4,
editable=False)
serial_number = models.CharField(max_length=100,
unique=True,
null=True)
name = models.CharField(max_length=50, blank=True, null=True)
group = models.ForeignKey('CameraGroup',
on_delete=models.SET_NULL,
null=True,
related_name='cameras',
related_query_name='cameras')
# last seen at ip
last_sighting_ip = models.CharField(max_length=50, blank=True, null=True)
last_sighting_time = models.DateTimeField(null=True, blank=True)
objects = CameraQuerySet.as_manager()
def __str__(self):
return self.name or self.serial_number or str(self.public_identifier)
def as_json(self):
return {
'serial_number': self.serial_number
}
我正在发送请求,并将其保存到数据库中,但是没有得到响应,我需要能够进行pytest并将其合并以进行开发。 我尝试使用form.save()form.save_m2m()保存它,因为我正在修改一些数据。我添加时间和IP地址(必须全部变小写,但一次只能做一件事) 当我不使用form.save()保存它时,会得到一个响应,因此这意味着我的数据在某种程度上无效,但是我无法弄清楚它到底是怎么回事。
我正在通过Restlet客户端发送请求,类似于Postman,如果我不提出请求,它会一直持续到无穷大...