NOT NULL constraint failed: api_userlog.browser_info_id (when i want to add show me this error but update and list are working fine)

北城余情 提交于 2020-06-01 04:45:56

问题


model.py

from django.db import models

from django.utils import timezone

Create your models here.

class PageOnHold(models.Model):

timestamp = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now)
spent_time = models.IntegerField(max_length=None,blank=True, null=True)
elapsed_time = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now)

class MouseClick(models.Model):

timestamp = models.DateTimeField(blank=True, null=True,default=timezone.now)
x_cord    = models.IntegerField(max_length=None,blank=True, null=True)
y_cord    = models.IntegerField(max_length=None,blank=True, null=True)

class MouseOver(models.Model):

timestamp = models.DateTimeField(blank=True, null=True,default=timezone.now)
x_cord    = models.IntegerField(max_length=None,blank=True, null=True)
y_cord    = models.IntegerField(max_length=None,blank=True, null=True)

class InteractionInfo(models.Model):

page_in_time = models.DateTimeField(blank=True, null=True,default=timezone.now)
page_on_hold = models.ForeignKey('PageOnHold',on_delete=models.CASCADE)
page_out_time = models.DateTimeField(blank=True, null=True,default=timezone.now)
estimated_time_spent = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now)
mouse_click = models.ForeignKey('MouseClick',on_delete= models.CASCADE)
mouse_over = models.ForeignKey('MouseOver',on_delete= models.CASCADE)

class NetworkInfo(models.Model):

city        = models.CharField(max_length=100)
country     = models.CharField(max_length=100)
hostname    = models.CharField(max_length=100)
latitude    = models.IntegerField(max_length=None)
longitude   = models.IntegerField(max_length=None)
org         = models.CharField(max_length=100)
postal      = models.IntegerField(max_length=None)
region      = models.CharField(max_length=100)
timezone    = models.DateTimeField(max_length=100,default=timezone.now)

def __str__(self):
    return self.city

class BrowserInfo(models.Model):

app_code         = models.CharField(max_length=100)
app_name         = models.CharField(max_length=100)
app_version      = models.CharField(max_length=100)
cookie_enabled   = models.BooleanField(default=False)
language         = models.CharField(max_length=100)
online           = models.BooleanField(default=False)
platform         = models.CharField(max_length=100)
# plugins          = models.ForeignKey(plugins) # another table  should be implement
user_agent       = models.CharField(max_length=100)
has_err          = models.BooleanField(default=False)


def __str__(self):
    return self.app_code

class GeoLocation(models.Model):

timestamp = models.DateTimeField(null=True,blank=True,default=timezone.now)
coords = models.ForeignKey('Coords',on_delete=models.CASCADE)
hasErr = models.BooleanField(default=False)
errCode = models.CharField(max_length=100)


def __str__(self):
    return self.errCode

class Coords(models.Model):

accuracy        = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')
altitude        = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')
altitudeAccuracy= models.CharField(max_length=100,blank=True, null=True,default='SOME STRING')
heading         = models.CharField(max_length=100,blank=True, null=True,default='SOME STRING')
latitude        = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')
longitude       = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')
speed           = models.IntegerField(max_length=None,blank=True, null=True,default='SOME STRING')

def __str__(self):
    return self.heading

class UserLog(models.Model):

IP = models.IntegerField(unique=True,null= False,blank = False)
session_id = models.IntegerField(max_length=None,null=False)
time_in   = models.DateTimeField(null=True, blank=True,default=timezone.now)
time_out   = models.DateTimeField(null=True, blank=True,default=timezone.now)
interaction_info = models.ForeignKey('InteractionInfo',on_delete=models.CASCADE)
network_info = models.ForeignKey('NetworkInfo',on_delete=models.CASCADE)
browser_info = models.ForeignKey('BrowserInfo',on_delete=models.CASCADE)
geoLocation = models.ForeignKey('GeoLocation',on_delete=models.CASCADE)
timestamp =models.DateTimeField(null=True,blank=True,default=timezone.now)
screen_width = models.CharField(max_length=100)
url= models.CharField(max_length=100)
browserCookie = models.CharField(max_length=100)

def __str__(self):
    return self.screen_width

serializer.py

from django.utils import timezone

from rest_framework import serializers

from .models import

UserLog,GeoLocation,BrowserInfo,Coords,InteractionInfo,MouseClick,MouseOver,PageOnHold,NetworkInfo

class CoordsSeializer(serializers.ModelSerializer):

accuracy        = serializers.IntegerField()
altitude        = serializers.IntegerField()
altitudeAccuracy= serializers.CharField()
heading         = serializers.CharField()
latitude        = serializers.IntegerField()
longitude       = serializers.IntegerField()
speed           = serializers.IntegerField()


class Meta:
    model = Coords
    fields = ('id','accuracy','altitude','altitudeAccuracy','heading','latitude','longitude','speed')
    depth = 8

class GeoLocationSerializer(serializers.ModelSerializer):

coords = CoordsSeializer(many = False,read_only=True)
# timestamp = serializers.DateTimeField()
# coords = CoordsSeializer() 
# hasErr = serializers.BooleanField()
# errCode = serializers.CharField()

class Meta:
    model = GeoLocation
    fields = ('id','timestamp','coords','hasErr','errCode')
    depth = 4

class BrowserInfoSerializer(serializers.ModelSerializer):

app_code         = serializers.CharField()
app_name         = serializers.CharField()
app_version      = serializers.CharField()
cookie_enabled   = serializers.BooleanField()
language         = serializers.CharField()
online           = serializers.BooleanField()
platform         = serializers.CharField()
# plugins          = models.ForeignKey(plugins) # another table  should be implement
user_agent       = serializers.CharField()
has_err          = serializers.BooleanField()

class Meta:
    model = BrowserInfo
    fields = ('id','app_code','app_name','app_version','cookie_enabled','language','online','platform','user_agent','has_err')
    depth = 10

class NetworkInfoSerializer(serializers.ModelSerializer):

city        = serializers.CharField()
country     = serializers.CharField()
hostname    = serializers.CharField()
latitude    = serializers.IntegerField()
longitude   = serializers.IntegerField()
org         = serializers.CharField()
postal      = serializers.IntegerField()
region      = serializers.CharField()
timezone    = serializers.DateTimeField()

class Meta:
    model = NetworkInfo
    fields = ('id','city','country','hostname','latitude','longitude','org','postal','region','timezone')
    depth = 10

class PageOnHoldserializer(serializers.ModelSerializer):

timestamp       = serializers.DateTimeField()
spent_time      = serializers.IntegerField()
elapsed_time    = serializers.DateTimeField()

class Meta:
    model = PageOnHold
    fields = ('id','timestamp','spent_time','elapsed_time')
    depth = 4

class MouseClickserializer(serializers.ModelSerializer):

timestamp = serializers.DateTimeField()
x_cord    = serializers.IntegerField()
y_cord    = serializers.IntegerField()

class Meta:
    model = MouseClick
    fields = ('id','timestamp','x_cord','y_cord')
    depth = 4

class MouseOverserializer(serializers.ModelSerializer):

timestamp = serializers.DateTimeField()
x_cord    = serializers.IntegerField()
y_cord    = serializers.IntegerField()

class Meta:
    model = MouseOver
    fields = ('id','timestamp','x_cord','y_cord')
    depth = 4

class InteractionInfoSerializer(serializers.ModelSerializer):

page_on_hold =PageOnHoldserializer(many=False,read_only=True)
mouse_click = MouseClickserializer(many=False,read_only=True)
mouse_over = MouseOverserializer(many=False,read_only=True)

class Meta:
    model = InteractionInfo
    fields = ('id','page_in_time','page_out_time','estimated_time_spent','page_on_hold','mouse_click','mouse_over')
    depth= 4

class UserLogSerializer(serializers.ModelSerializer):

interaction_info =InteractionInfoSerializer(many=False,read_only=True) 
network_info     =  NetworkInfoSerializer(many=False,read_only=True)
browser_info      = BrowserInfoSerializer(many=False,read_only=True)
geoLocation       = GeoLocationSerializer(many=False,read_only=True)


class Meta:
    model = UserLog
    fields = ('id','IP','session_id','time_in','time_out','timestamp','screen_width','url','browserCookie','interaction_info','network_info','browser_info','geoLocation')
    depth =9

views.py

from django.shortcuts import render

from django.http import HttpResponse

from django.shortcuts import get_list_or_404

from rest_framework.views import APIView

from rest_framework.response import Response

from rest_framework.decorators import api_view

from rest_framework import status

from .models import UserLog,GeoLocation,BrowserInfo,Coords,InteractionInfo,MouseClick,MouseOver,PageOnHold,NetworkInfo

from .serializers import UserLogSerializer,GeoLocationSerializer,BrowserInfoSerializer,CoordsSeializer,InteractionInfoSerializer,MouseClickserializer,MouseOverserializer,PageOnHoldserializer,NetworkInfoSerializer

class userDetailsList(APIView):

def get(self,request):
    user = UserLog.objects.all()
    serializers = UserLogSerializer(user,many=True)
    return Response(serializers.data)

class userLogCreate(APIView):

def post(self,request):

    serilizer = UserLogSerializer(data=request.data)

    if serilizer.is_valid():
        serilizer.save()
    return Response(serilizer.data)

class userLogUpdate(APIView):

def post(self,request,pk):

    user = UserLog.objects.get(id=pk)
    serializer = UserLogSerializer(instance=user,data=request.data)
    if serializer.is_valid():
        serializer.save()

    return Response(serializer.data)

来源:https://stackoverflow.com/questions/61519437/not-null-constraint-failed-api-userlog-browser-info-id-when-i-want-to-add-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!