IntegrityError when sending multiple post requests - django

I am sending a post request for each tag
front-end is here:
handleOnClick() {
const { resource, csrf } = this.props;
let postFunc = (ele) => {
const label = ele.label.toLowerCase()
request
.post(`/bookmarks/groups/${label}`)
.set('X-CSRFToken', csrf)
.send(resource.id).then(() => {
console.log('we did it')
})
}
_(this.state.multiValue).forEach(postFunc)
Both post request seem to deliver, but any one after the first get and integrity error.
My view:
def post(self, request, slug):
"""Create a bookmark of the resource for the current user."""
# print request.user
to_add = Resource.objects.get(id=int(request.body))
try:
bm = Bookmark.objects.get(resource=to_add)
except Bookmark.DoesNotExist:
bm = Bookmark.objects.create(user=request.user, resource=to_add)
bookmark_group = BookmarkGroup.objects.get(name=slug)
bookmark_group.bookmarks.add(bm)
return JsonResponse({}, status=201, safe=False)
I am essentially checking to see if a bookmark is exists, and if it doesn't I create it.
Bookmark and group models:
class Bookmark(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
resource = models.ForeignKey("Resource", on_delete=models.CASCADE, related_name="bookmarks")
# meta
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
unique_together = ('user', 'resource',)
ordering = ('resource',)
def __str__(self):
return "{} | {}".format(self.user, self.resource)
class BookmarkGroup(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(null=False, unique=True)
bookmarks = models.ManyToManyField(Bookmark, related_name='bookmarks', blank=True)
def __str__(self):
return self.name
I've included the stack-trace, and can include more code if needed:
Traceback (most recent call last):
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Users/path/to/my/app/views/bookmark_group.py", line 37, in post
bm = Bookmark.objects.create(user=request.user, resource=to_add)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/models/base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/models/base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/models/base.py", line 922, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/models/base.py", line 961, in _do_insert
using=using, raw=raw)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/models/query.py", line 1061, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
cursor.execute(sql, params)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/debug_toolbar/panels/sql/tracking.py", line 164, in execute
return self._record(self.cursor.execute, sql, params)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/debug_toolbar/panels/sql/tracking.py", line 106, in _record
return method(sql, params)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 101, in execute
return self.cursor.execute(query, args)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/Users/path/to/my/venv/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry '1-2728' for key 'app_bookmark_user_id_resource_id_a01d1abf_uniq'")

From your traceback this line causing the exception,
bm = Bookmark.objects.create(user=request.user, resource=to_add)Ther error is because of you defined unique_together = ('user', 'resource',).
I would suggest you to change your views.py as below
def post(self, request, slug):
"""Create a bookmark of the resource for the current user."""
to_add = Resource.objects.get(id=int(request.body))
# Changes starts
bm, created = Bookmark.objects.get_or_create(user=request.user, resource=to_add)
bookmark_group = BookmarkGroup.objects.get(name=slug)
bookmark_group.bookmarks.add(bm)
return JsonResponse({}, status=201, safe=False)
or you can simply change ,
bm = Bookmark.objects.get(resource=to_add) to bm = Bookmark.objects.get(resource=to_add,user=request.user)

Related

DataError('value too long for type character varying(40) on Heroku but not on development

My project works fine under the development but got this error on Heroku. The error occurs when user try to register. I use registration redux
Traceback (most recent call last):
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
The above exception (value too long for type character varying(40)
) was the direct cause of the following exception:
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\debug.py", line 89, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\registration\views.py", line 53, in dispatch
return super().dispatch(request, *args, **kwargs)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 142, in post
return self.form_valid(form)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\registration\views.py", line 56, in form_valid
new_user = self.register(form)
File "C:\Users\sdgy\SaaS\sdg\sdgenv\users\views.py", line 44, in register
new_user = super(Register, self).register(form_class)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\registration\backends\default\views.py", line 96, in register
new_user = self.registration_profile.objects.create_inactive_user(
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\registration\models.py", line 185, in create_inactive_user
registration_profile = self.create_profile(
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\registration\models.py", line 211, in create_profile
profile.save()
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
cursor.execute(sql, params)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\debug_toolbar\panels\sql\tracking.py", line 198, in execute
return self._record(self.cursor.execute, sql, params)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\debug_toolbar\panels\sql\tracking.py", line 133, in _record
return method(sql, params)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\sdgy\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
Exception Type: DataError at /accounts/register/
Exception Value: value too long for type character varying(40)
I just made some updates including Python39 and Django 3. So maybe a lot have changed. But if the local environment is working fine, why does Heroku have issues?
My view
class Register(RegistrationView):
form_class = UserRegistrationForm
def register(self, form_class):
new_user = super(Register, self).register(form_class)
profile = Profile()
profile.user = new_user
profile.company = form_class.cleaned_data['company']
profile.title = form_class.cleaned_data['title']
profile.phone_number = form_class.cleaned_data['phone_number']
profile.address = form_class.cleaned_data['address']
profile.city = form_class.cleaned_data['city']
profile.province = form_class.cleaned_data['province']
profile.country = form_class.cleaned_data['country']
profile.postal = form_class.cleaned_data['postal']
profile.save()
template = get_template('registration/registration.txt')
context = {'name':new_user.first_name,'last':new_user.last_name,'company':profile.company,
'phone':profile.phone_number}
content = template.render(context)
return redirect('registration_complete')
My model
class Profile(models.Model):
user = models.OneToOneField(User, max_length=250,on_delete=models.CASCADE)
city=models.CharField(max_length=250, blank=True)
province=models.CharField(max_length=250, blank=True)
country=models.CharField(max_length=250,blank=True)
postal=models.CharField(max_length=7, blank=True)
address = models.CharField(max_length=250, blank=True)
company = models.CharField(max_length=250, blank=True)
title = models.CharField(max_length=250, blank=True)
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
message="Phone number must be entered in the format: '+6041234567'. Up to 15 digits allowed.")
phone_number = models.CharField(validators=[phone_regex], max_length=15, blank=True)
def __str__(self):
return 'Profile for user {}'.format(self.user.username)
I am learning here so please give me any pointers you might find helpful. Thanks.

Integrity Error django with UniqueConstraint

I have the following model snippet:
class InvoiceReference(TemplateMixin, models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
reference_prefix = models.CharField(
max_length=100,
validators=(RegexValidator(regex='^\S+$',
message='reference prefix must not include spaces'),)
)
reference_offset = models.PositiveIntegerField(default=0)
reference_suffix = models.CharField(
max_length=100,
validators=(RegexValidator(regex='^\S+$',
message='reference suffix must not include spaces'),)
)
reference_separator = models.CharField(max_length=1)
class Meta:
constraints = [
models.UniqueConstraint(fields=('user', 'reference_prefix', 'reference_suffix', 'reference_separator',), name='unique_reference')
]
The idea is to have it so that a no single user cannot use the same reference_prefix, reference_separator and reference_suffix together multiple times. (if different users use the same combination of reference_prefix, reference_separator and reference_suffix this is ok.)
I am currently using the django-admin model form, where I have excluded the user field and attach a user in save_model:
class InvoiceReferenceAdmin(admin.ModelAdmin):
model = InvoiceReference
exclude = ('user',)
def save_model(self, request, obj, form, change):
if not obj.pk:
obj.user = request.user
super().save_model(request=request, obj=obj, form=form, change=change)
however, this results in an 'Integrity Error' when I try to save the model.
Traceback:
Traceback (most recent call last):
File "django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: invoice_invoice.user_id, invoice_invoice.reference_prefix, invoice_invoice.reference_suffix, invoice_invoice.reference_separator
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "django/contrib/admin/options.py", line 606, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "django/contrib/admin/sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "django/contrib/admin/options.py", line 1645, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "django/utils/decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "django/contrib/admin/options.py", line 1529, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "django/contrib/admin/options.py", line 1572, in _changeform_view
self.save_model(request, new_object, form, not add)
File "/Users/leonhughes/dev/django/invoiceweb/invoice/admin.py", line 42, in save_model
super().save_model(request=request, obj=obj, form=form, change=change)
File "django/contrib/admin/options.py", line 1088, in save_model
obj.save()
File "django/db/models/base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "django/db/models/base.py", line 779, in save_base
force_update, using, update_fields,
File "django/db/models/base.py", line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "django/db/models/base.py", line 908, in _do_insert
using=using, raw=raw)
File "django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "django/db/models/query.py", line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "django/db/models/sql/compiler.py", line 1375, in execute_sql
cursor.execute(sql, params)
File "django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: invoice_invoice.user_id, invoice_invoice.reference_prefix, invoice_invoice.reference_suffix, invoice_invoice.reference_separator
I have seen this in the django documentation
In general constraints are not checked during full_clean(), and do not
raise ValidationErrors. Rather you’ll get a database integrity error
on save(). UniqueConstraints without a condition (i.e. non-partial
unique constraints) are different in this regard, in that they
leverage the existing validate_unique() logic, and thus enable
two-stage validation. In addition to IntegrityError on save(),
ValidationError is also raised during model validation when the
UniqueConstraint is violated.
As I have no conditions set shouldn't a ValidationError be raised first?
One way to achieve a similar result as yours would be to make user a hidden field in your admin form and then override ModelAdmin.get_changeform_initial_data to pass the current user as the value for this hidden input.
This would mean that the user would be included in unique validation checks as the field would not be excluded from the form. When editing an existing object initial data is ignored so you shouldn't override any existing user relation
class InvoiceReferenceAdminForm(forms.ModelForm):
class Meta:
model = InvoiceReference
fields = '__all__'
widgets = {'user': forms.HiddenInput}
class InvoiceReferenceAdmin(admin.ModelAdmin):
model = InvoiceReference
form = InvoiceReferenceAdminForm
def get_changeform_initial_data(self, request):
return {'user': request.user}

In Django REST framework, how do I tell my serializer to update a member field instead of trying to create it from scratch?

I'm using Django 3 and Python 3.8 along with the Django rest framework. I have the following model. Notice the many-to-many relationship with addresses ...
class Coop(models.Model):
objects = CoopManager()
name = models.CharField(max_length=250, null=False)
types = models.ManyToManyField(CoopType, blank=False)
addresses = models.ManyToManyField(Address)
enabled = models.BooleanField(default=True, null=False)
phone = models.ForeignKey(ContactMethod, on_delete=models.CASCADE, null=True, related_name='contact_phone')
email = models.ForeignKey(ContactMethod, on_delete=models.CASCADE, null=True, related_name='contact_email')
web_site = models.TextField()
I have created the following serializers. I'm having trouble with updating the model so I exluded the create methods for simplicity ...
class AddressTypeField(serializers.PrimaryKeyRelatedField):
queryset = Address.objects
def to_internal_value(self, data):
if type(data) == dict:
locality = data['locality']
state = None if not re.match(r"[0-9]+", str(locality['state'])) else State.objects.get(pk=locality['state'])
locality['state'] = state
locality, created = Locality.objects.get_or_create(**locality)
data['locality'] = locality
address = Address.objects.create(**data)
# Replace the dict with the ID of the newly obtained object
data = address.pk
return super().to_internal_value(data)
...
class CoopSerializer(serializers.ModelSerializer):
types = CoopTypeSerializer(many=True, allow_empty=False)
addresses = AddressTypeField(many=True)
phone = ContactMethodPhoneSerializer()
email = ContactMethodEmailSerializer()
class Meta:
model = Coop
fields = '__all__'
...
def update(self, instance, validated_data):
"""
Update and return an existing `Coop` instance, given the validated data.
"""
instance.name = validated_data.get('name', instance.name)
try:
coop_types = validated_data['types']
instance.types.clear() # Disassociates all CoopTypes from instance.
for item in coop_types:
coop_type, _ = CoopType.objects.get_or_create(**item)
instance.types.add(coop_type)
except KeyError:
pass
instance.addresses = validated_data.get('addresses', instance.addresses)
instance.enabled = validated_data.get('enabled', instance.enabled)
phone = validated_data.pop('phone', {})
email = validated_data.pop('email', {})
instance.phone = ContactMethod.objects.create(type=ContactMethod.ContactTypes.PHONE, **phone)
instance.email = ContactMethod.objects.create(type=ContactMethod.ContactTypes.EMAIL, **email)
instance.web_site = validated_data.get('web_site', instance.web_site)
instance.web_site = validated_data.get('web_site', instance.web_site)
instance.save()
return instance
What's the right way to handle updating the address fields of my main Coop model? When I submit a payload like below (I have IDs included with my address member field objects) ...
{"id":927,"types":[{"id":76,"name":"ct"}],"addresses":[{"id":824,"street_number":"","route":"","raw":"","formatted":"4750 N Woodlawn Rd","latitude":null,"longitude":null,"locality":{"id":54,"name":"Chicago","postal_code":"60640","state":19313}}],"phone":{"phone":"3039468888"},"email":{"email":"dave#hello.com"},"name":"Dave Coop","enabled":true,"web_site":"http://www.example.org"}
And submit it via PATCH, I get the error below. Is there some way to tell my serializer to only update the member field if an ID is created instead of trying to create it from scratch each time?
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/web/directory/views.py", line 78, in put
if serializer.is_valid():
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/serializers.py", line 234, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/serializers.py", line 433, in run_validation
value = self.to_internal_value(data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/serializers.py", line 490, in to_internal_value
validated_value = field.run_validation(primitive_value)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/fields.py", line 565, in run_validation
value = self.to_internal_value(data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/relations.py", line 519, in to_internal_value
return [
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rest_framework/relations.py", line 520, in <listcomp>
self.child_relation.to_internal_value(item)
File "/Users/davea/Documents/workspace/chicommons/maps/web/directory/serializers.py", line 35, in to_internal_value
address = Address.objects.create(**data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 433, in create
obj.save(force_insert=True, using=self.db)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 745, in save
self.save_base(using=using, force_insert=force_insert,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 782, in save_base
updated = self._save_table(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 886, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 923, in _do_insert
return manager._insert(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1377, in execute_sql
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 74, in execute
return self.cursor.execute(query, args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
django.db.utils.IntegrityError: (1062, "Duplicate entry '824' for key 'address_address.PRIMARY'")
try using partial=True in serializer

Generate unique string for each db object

I have a model where I need to create a UNIQUE string for each object in the db.
I've made a function to generate the string, but I can create an object using the django admin once, and then the second time it will error out and say that the value is not unique.
What can I do so Django just generates a new string if the previous one was used already?
models.py
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
class Refferal(models.Model):
email = models.EmailField(max_length=254, unique=True)
coupon = models.CharField(default=randomword(4), primary_key=True, editable=False, max_length=4)
count = models.IntegerField(default=0)
Log output from when I try to create the second object using the admin
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 607, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 231, in inner
return view(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1638, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1522, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1565, in _changeform_view
self.save_model(request, new_object, form, not add)
File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1081, in save_model
obj.save()
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 746, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 784, in save_base
force_update, using, update_fields,
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 926, in _do_insert
using=using, raw=raw,
File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1384, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "index_refferal_coupon_a6e4bc31_pk"
DETAIL: Key (coupon)=(vtgs) already exists.
You here will evaluate eagerly the call randomword(4), and set the result of that function call as default value. You can however pass a callable:
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def random4():
return randomword(4)
class Refferal(models.Model):
email = models.EmailField(max_length=254, unique=True)
coupon = models.CharField(default=random4, primary_key=True, editable=False, max_length=4)
count = models.IntegerField(default=0)
Note that we do not call the function. We only pass a reference to the function.
That being said, it might be better to use Django's UUIDField [Django-doc]. This is made to store and generate random UUIDs, which are likely better sources of random strings.

Django one-one relation IntegrityError: UNIQUE constraint failed

I want to make a website which creates a 14 weeks therapy session for its users.
The course/therapy has a one to one relationship with the user and the therapist. And a many to one relationship with the weekly sessions model. When I simply created the course without creating the weekly sessions, everything worked fine. But when I added code for creating weekly sessions(in the same views function), it shows integrity error.
models.py :
class Therapist(models.Model):
name = models.CharField(max_length=20)
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
contact = models.CharField(validators=[phone_regex], max_length=17,blank=True)
region=models.CharField(max_length=30,default='online')
def __unicode__(self):
return self.name
class UserProfile(models.Model):
user = models.OneToOneField(User)
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone = models.CharField(validators=[phone_regex],max_length=17,blank=True)
age=models.IntegerField()
region=models.CharField(max_length=30)
def __unicode__(self):
return self.user.username
def get_region(self):
return self.region
class CBT_therapy(models.Model):
"""docstring for CBT_therapy : has one to one relationship with user and therapist.
Many to one relation with weekly session.
"""
start_date = models.DateField()
session_time=models.TimeField()
therapist=models.OneToOneField(Therapist)
user=models.OneToOneField(User,on_delete=models.CASCADE)
class WeeklySession(models.Model):
session_date=models.DateField()
session_time=models.TimeField()
week_no = models.IntegerField()
challenge=models.CharField(max_length=150)
therapy = models.ForeignKey(CBT_therapy)
class Challenge(models.Model):
title=models.CharField(max_length=150)
def __unicode__(self):
return self.title
Views.py :
def registerCBT(request):
if request.method == 'POST':
register_form = RegisterCBTForm(request.POST or None)
if register_form.is_valid():
start_date=register_form.cleaned_data['start_date']
session_time=register_form.cleaned_data['session_time']
user=request.user
username=UserProfile.objects.get(user=user)
region=username.get_region()
try:
therapist_name=Therapist.objects.get(region=region)
except :
therapist_name=Therapist.objects.get(region='online')
cbt=CBT_therapy(
user=user,
start_date=start_date,
session_time=session_time,
therapist=therapist_name
)
cbt.save(force_insert=True)
session_date=start_date
for challenge in Challenge.objects.all():
session_date = session_date + timedelta(days=7)
w=WeeklySession(
session_time =session_time,
session_date =session_date,
week_no=challenge.pk,
challenge=challenge.title,
therapy=cbt
)
w.save(force_insert=True)
else:
register_form = RegisterCBTForm()
return render(request,'register_for_cbt.html',{'register_form':register_form})
Traceback :
Traceback (most recent call last):
File "/home/tux/yes/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: CBT_cbt_therapy.therapist_id
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/tux/yes/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/tux/yes/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/tux/yes/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/tux/frames/CBT_therapy/CBT/views.py", line 63, in registerCBT
cbt.save(force_insert=True)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/models/base.py", line 837, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/models/base.py", line 923, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/models/base.py", line 962, in _do_insert
using=using, raw=raw)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1107, in execute_sql
cursor.execute(sql, params)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/tux/yes/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/tux/yes/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: CBT_cbt_therapy.therapist_id