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
I have an app that will one day allow front-end crud, which will create the slug with slugify. Right now though, all the object creation is being done in the admin area and I was wondering if there is a way to auto generate slugs while creating and saving an object from within admin?
Here is the method for slugify for the front-end; not sure if its even relevant. Thank you.
def create_slug(instance, new_slug=None):
slug = slugify(instance.title)
if new_slug is not None:
slug = new_slug
qs = Veteran.objects.filter(slug=slug).order_by('-id')
exists = qs.exists()
if exists:
new_slug = '%s-%s' % (slug, qs.first().id)
return create_slug(instance, new_slug=new_slug)
return slug
Having just used this on another answer, I have exactly the right code in my clipboard. I do exactly this for one of my models:
from django.utils.text import slugify
class Event(models.Model):
date = models.DateField()
location_title = models.TextField()
location_code = models.TextField(blank=True, null=True)
picture_url = models.URLField(blank=True, null=True, max_length=250)
event_url = models.SlugField(unique=True, max_length=250)
def __str__(self):
return self.event_url + " " + str(self.date)
def save(self, *args, **kwargs):
self.event_url = slugify(self.location_title+str(self.date))
super(Event, self).save(*args, **kwargs)
Above solutions break validation in the Django Admin interface. I suggest:
from django import forms
from django.http.request import QueryDict
from django.utils.text import slugify
from .models import Article
class ArticleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ArticleForm, self).__init__(*args, **kwargs)
# Ensure that data is a regular Python dictionary so we can
# modify it later.
if isinstance(self.data, QueryDict):
self.data = self.data.copy()
# We assume here that the slug is only generated once, when
# saving the object. Since they are used in URLs they should
# not change when valid.
if not self.instance.pk and self.data.get('title'):
self.data['slug'] = slugify(self.data['title'])
class Meta:
model = Article
exclude = []
I have a Department table. And a Student table which references the department table. I have also created a custom user model,UserAugment, to add department field to the default User model.
class Department(models.Model):
dname = models.CharField(max_length=200,primary_key=True)
def publish(self):
self.save()
def __str__(self):
return self.dname
class UserAugment(AbstractUser):
department = models.ForeignKey(Department,null=True,blank=True)
objects = UserManager()
class Student(models.Model):
sname = models.CharField(max_length=200)
usn = models.CharField(max_length=200,primary_key=True)
dept = models.ForeignKey(Department)
sem = models.IntegerField()
def publish(self):
self.save()
def __str__(self):
return '%s, %s' % (self.usn,self.sname)
Now, I need to create users, who would have a department attached to them. And each user should be given permission for add/edit/view for students belonging to their department. And I'm having a nightmare doing that. Was it really simple? Thanks for any help that would pour out.
Well,this was what worked finally for me,
class StudentAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super(StudentAdmin, self).get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(dept=request.user.department)
You can manage permission control in the admin:
class UserAdmin(admin.ModelAdmin):
def has_change_permission(self, request, obj=None):
return self.user_has_permission(request, obj)
def user_has_permission(self, request, obj):
if hasattr(request.user, 'department') and obj is not None:
return request.user.department == obj.dept
return False
You can implement the edit and delete permissions the same way.
I have these two models:
class probe(models.Model):
serial=models.CharField("Serial Number",max_length=30,primary_key=True)
clientID=models.ForeignKey(organisation)
inst_date=models.DateField("Installation Date")
exp_date=models.DateField("Expiration Date",blank=True)
def save(self):
if self.exp_date is None:
self.exp_date=self.inst_date.replace(year=self.inst_date.year+1)
super(probe,self).save()
def isExpired(self):
return self.exp_date<=datetime.date.today()
isExpired.admin_order_field="exp_date"
isExpired.boolean=True
isExpired.short_description="Needs calibration"
def __str__(self):
return self.serial
class calibration(models.Model):
probe=models.ForeignKey(probe)
date=models.DateField("Date of Calibration")
isSent=models.BooleanField("Email sent?",default=False)
def __str__(self):
return str(self.date)
def save(self):
self.probe.exp_date=self.date.replace(year=self.date.year+1)
super(calibration, self).save()
When I create a calibration, I want the expiry date of the probe to update, how can I implement my models to allow that?
Try this:
from dateutil.relativedelta import relativedelta
def save(self):
self.probe.exp_date=self.date + relativedelta(years=1)
self.probe.save()
super(calibration, self).save()
I am trying to create a unique slug in Django so that I can access a post via a url like this:
http://www.example.com/buy-a-new-bike_Boston-MA-02111_2
The relevant models:
class ZipCode(models.Model):
zipcode = models.CharField(max_length=5)
city = models.CharField(max_length=64)
statecode = models.CharField(max_length=32)
class Need(models.Model):
title = models.CharField(max_length=50)
us_zip = models.CharField(max_length=5)
slug = ?????
def get_city():
zip = ZipCode.objects.get(zipcode=self.us_zip)
city = "%s, %s %s" % (zip.city, zip.statecode, zip.zipcode)
return city
A sample ZipCode record:
zipcode = "02111"
city = "Boston"
statecode = "MA"
A sample Need record:
title = "buy a new bike"
us_zip = "02111"
slug = "buy-a-new-bike_Boston-MA-02111_2" (desired)
Any tips as to how to create this unique slug? Its composition is:
Need.title + "_" + Need.get_city() + "_" + an optional incrementing integer to make it unique. All spaces should be replaced with "-".
NOTE: My desired slug above assumes that the slug "buy-a-new-bike_Boston-MA-02111" already exists, which is what it has the "_2" appended to it to make it unique.
I've tried django-extensions, but it seems that it can only take a field or tuple of fields to construct the unique slug. I need to pass in the get_city() function as well as the "_" connector between the title and city. Anyone solved this and willing to share?
Thank you!
UPDATE
I'm already using django-extensions for its UUIDField, so it would be nice if it could also be usable for its AutoSlugField!
I use this snippet for generating unique slug and my typical save method look like below
slug will be Django SlugField with blank=True but enforce slug in save method.
typical save method for Need model might look below
def save(self, **kwargs):
slug_str = "%s %s" % (self.title, self.us_zip)
unique_slugify(self, slug_str)
super(Need, self).save(**kwargs)
and this will generate slug like buy-a-new-bike_Boston-MA-02111 , buy-a-new-bike_Boston-MA-02111-1 and so on. Output might be little different but you can always go through snippet and customize to your needs.
My little code:
def save(self, *args, **kwargs):
strtime = "".join(str(time()).split("."))
string = "%s-%s" % (strtime[7:], self.title)
self.slug = slugify(string)
super(Need, self).save()
If you are thinking of using an app to do it for you, here is one.
https://github.com/un33k/django-uuslug
UUSlug = (``U``nique + ``U``code Slug)
Unicode Test Example
=====================
from uuslug import uuslug as slugify
s = "This is a test ---"
r = slugify(s)
self.assertEquals(r, "this-is-a-test")
s = 'C\'est déjà l\'été.'
r = slugify(s)
self.assertEquals(r, "c-est-deja-l-ete")
s = 'Nín hǎo. Wǒ shì zhōng guó rén'
r = slugify(s)
self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")
s = '影師嗎'
r = slugify(s)
self.assertEquals(r, "ying-shi-ma")
Uniqueness Test Example
=======================
Override your objects save method with something like this (models.py)
from django.db import models
from uuslug import uuslug as slugify
class CoolSlug(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=200)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = slugify(self.name, instance=self)
super(CoolSlug, self).save(*args, **kwargs)
Test:
=====
name = "john"
c = CoolSlug.objects.create(name=name)
c.save()
self.assertEquals(c.slug, name) # slug = "john"
c1 = CoolSlug.objects.create(name=name)
c1.save()
self.assertEquals(c1.slug, name+"-1") # slug = "john-1"
Here are a couple functions that I use. You pass in the model instance and the desired title into unique_slugify which will add the slug if it doesn't exist, otherwise it will continue trying to append a 4 digit random string until it finds a unique one.
import string
from django.utils.crypto import get_random_string
def unique_slugify(instance, slug):
model = instance.__class__
unique_slug = slug
while model.objects.filter(slug=unique_slug).exists():
unique_slug = slug + get_random_string(length=4)
return unique_slug
I usually use it by overriding the model save method.
class YourModel(models.Model):
slug = models.SlugField()
title = models.CharField()
def save(self, *args, **kwargs):
if not self.slug:
self.slug = unique_slugify(self, slugify(self.title))
super().save(*args, **kwargs)
This is the simple and small code i am using for generating unique slug,
you only need one field to create your unique slug field
from random import randint
def save(self, *args, **kwargs):
if Post.objects.filter(title=self.title).exists():
extra = str(randint(1, 10000))
self.slug = slugify(self.title) + "-" + extra
else:
self.slug = slugify(self.title)
super(Post, self).save(*args, **kwargs)
I hope you like this.
This is a simple implementation that generate the slug from the title, it doesn't depend on other snippets:
from django.template.defaultfilters import slugify
class Article(models.Model):
...
def save(self, **kwargs):
if not self.slug:
slug = slugify(self.title)
while True:
try:
article = Article.objects.get(slug=slug)
if article == self:
self.slug = slug
break
else:
slug = slug + '-'
except:
self.slug = slug
break
super(Article, self).save()
Django provides a SlugField model field to make this easier for you. Here's an example of it in a "blog" app's
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField(blank=True)
slug = models.SlugField(unique=True)
#models.permalink
def get_absolute_url(self):
return 'blog:post', (self.slug,)
Note that we've set unique=True for our slug field — in this project we will be looking up posts by their slug, so we need to ensure they are unique. Here's what our application's views.py might look like to do this:
from .models import Post
def post(request, slug):
post = get_object_or_404(Post, slug=slug)
return render(request, 'blog/post.html', {
'post': post,
})
from django.utils.text import slugify Helps a lot and has quite clear Concepts.
Here one example on How to auto-generate slug by using from django.utils.text import slugify
utils.py
from django.utils.text import slugify
import random
import string
# Random string generator
def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
# Unique Slug Generator
def unique_slug_generator(instance, new_slug=None):
"""
It assumes your instance has a model with a slug field and a title character (char) field.
"""
if new_slug is not None:
slug = new_slug
else:
slug = slugify(instance.title)
Klass = instance.__class__
qs_exists = Klass.objects.filter(slug=slug).exists()
if qs_exists:
new_slug = "{slug}-{randstr}".format(slug=slug, randstr=random_string_generator(size=4))
return unique_slug_generator(instance, new_slug=new_slug)
return slug
models.py
from django.db.models.signals import pre_save # Signals
# import the unique_slug_generator from .utils.py
from .utils import unique_slug_generator
class Product(models.Model):
title = models.CharField(max_length=120)
# set blank to True
slug = models.SlugField(blank=True, unique=True)
def product_pre_save_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = unique_slug_generator(instance)
pre_save.connect(product_pre_save_receiver, sender=Product)
Django documentation explains Django.utils.text import slugify to generate slug automatically. You can read more detail here
After implementing the code, while creating product, you may leave the slug field blank, which will be further aquired with auto generated slug for the product which will be unique in this case.
Hi can you tried this function
class Training(models.Model):
title = models.CharField(max_length=250)
text = models.TextField()
created_date = models.DateTimeField(
auto_now_add=True, editable=False, )
slug = models.SlugField(unique=True, editable=False, max_length=250)
def __unicode__(self):
return self.title
def get_unique_slug(id,title,obj):
slug = slugify(title.replace('ı', 'i'))
unique_slug = slug
counter = 1
while obj.filter(slug=unique_slug).exists():
if(obj.filter(slug=unique_slug).values('id')[0]['id']==id):
break
unique_slug = '{}-{}'.format(slug, counter)
counter += 1
return unique_slug.
def save(self, *args, **kwargs):
self.slug =self.get_unique_slug(self.id,self.title,Training.objects)
return super(Training, self).save(*args, **kwargs)
def get_slug(self):
slug = slugify(self.title.replace("ı", "i"))
unique = slug
number = 2
while Model.objects.filter(slug=unique).exists():
unique = "{}-{}".format(slug, number)
number += 1
return unique
Best solution for me:
def get_slug(self):
slug = slugify(self.title)
unique_slug = slug
number = 1
while Recipe.objects.filter(slug=unique_slug).exists():
unique_slug = f'{slug}-{number}'
number += 1
return unique_slug
def save(self, *args, **kwargs):
if not self.slug:
self.slug = self.get_slug()
return super().save(*args, **kwargs)
This code can generate slug like this:
string-slug
string-slug-1 (if previous alredy exists)
string-slug-2 (if previous alredy exists)
and so on...
class Need(models.Model):
title = models.CharField(max_length=50)
us_zip = models.CharField(max_length=5)
slug = models.SlugField(unique=True)
def save(self, **kwargs):
slug_str = "%s %s" % (self.title, self.us_zip)
super(Need, self).save()
Try this, worked out for me,welcome in advance:
class Parcel(models.Model):
title = models.CharField(max_length-255)
slug = models.SlugField(unique=True, max_length=255)
weight = models.IntegerField()
description = models.CharField(max_length=255)
destination = models.CharField(max_length=255)
origin = models.CharField(max_length=255)
def __str__(self):
return self.description
def save(self, *args, **kwargs):
if not self.slug:
t_slug = slugify(self.title)
startpoint = 1
unique_slug = t_slug
while Parcel.objects.filter(slug=unique_slug).exists():
unique_slug = '{} {}'.format(t_slug, origin)
origin += 1
self.slug = unique_slug
super().save(*args, **kwargs)