I am facing a problem in which I cannot save documents inside MongoDB with Django. The error follows:
AttributeError: 'NoneType' object has no attribute 'attname'
With the help of the library Djongo I made these models:
from djongo import models
from django.utils import timezone
class LatLng(models.Model):
latitude = models.FloatField(null=False)
longitude = models.FloatField(null=False,)
def __init__(self,latitude, longitude):
self.latitude = latitude
self.longitude = longitude
class Meta:
abstract = True
class Parameters(models.Model):
cond1= models.IntegerField(null=False,)
cond2= models.IntegerField(null=False,)
cond3= models.IntegerField()
class Meta:
abstract = True
class MyModel(models.Model):
name = models.CharField(max_length=150, null=False)
des= models.CharField(max_length=500)
pub_date = models.DateTimeField(editable=False)
mod_date = models.DateTimeField()
parameters = models.EmbeddedField(
model_container=Parameters
)
wp= models.ArrayField(
model_container=LatLng,
null=False
)
objects = models.DjongoManager()
def __init__(self, name, parameters, wp,des=""):
self.name = name
self.parameters = parameters
self.waypoints = waypoints
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if self.id is None:
self.pub_date = timezone.now()
self.mod_date = timezone.now()
return super(MyModel, self).save(*args, **kwargs)
def __str__(self):
return self.name
My API looks like:
def get_wp(pls):
wps = []
for pin pls:
coord = LatLng(latitude=p['latitude'], longitude=p['longitude'])
wps.append(coord)
return wps
@api_view(['POST'])
def save(request):
data = json.loads(request.body.decode('utf-8'))
scores = Parameters(cond1=data['cond1'], cond2=data['cond2'])
wps = get_wp(data['pls'])
obj = MyModel(name=data['name'],parameters=scores, waypoints=wps)
print("--> {}".format(obj.name)) #working fine
itinerary.save() ## it dies here
return JsonResponse({})
I don't know what I'm making wrong. Since this is my first time with Django (using MongoDB), any suggestions about my code are really appreciated.
Try removing LatLng __init__()
or if still needed then try:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Do your changes here