How to fix VersionConflict locking failure in pipenv?

Here are my debugging notes. Still not sure which package is causing the problem, but this does seem to fix it. The error you get when you first run pipenv install with pipenv version 2020.8.13. Traceback (most recent call last): File “/usr/local/bin/pipenv”, line 8, in <module> sys.exit(cli()) File “/usr/local/lib/python3.6/site-packages/pipenv/vendor/click/core.py”, line 829, in __call__ return self.main(*args, … Read more

Django: When extending User, better to use OneToOneField(User) or ForeignKey(User, unique=True)?

The only real reason given in the article is that it can be set up so that the admin page for User will show both the fields in User and UserProfile. This can be replicated with a OneToOneField with a little elbow grease, so unless you’re addicted to showing it in the admin page with … Read more

Django formset_factory vs modelformset_factory vs inlineformset_factory

The difference between the 3 formset factories is basically: formset_factory lets you render a bunch of forms together, but these forms are NOT necessarily related to a particular database models (this is not what you need, since you have models for everything) modelformset_factory lets you create/edit a bunch of Django model objects together, for example, … Read more

What’s the difference between returning a `HttpResponseNotFound` and raising a `Http404` in Django?

An HttpResponseNotFound is just like a normal HttpResponse except it sends error code 404. So it’s up to you to render an appropriate 404 page in that view, otherwise the browser will display its own default. Raising an Http404 exception will trigger Django to call its own 404 error view. Actually, this does little more … Read more

How to validate uniqueness constraint across foreign key (django)

Methods are not called on their own when saving the model. One way to do this is to have a custom save method that calls the validate_unique method when a model is saved: class Room(models.Model): zone = models.ForeignKey(Zone) name = models.CharField(max_length=255) def validate_unique(self, exclude=None): qs = Room.objects.filter(name=self.name) if qs.filter(zone__site=self.zone__site).exists(): raise ValidationError(‘Name must be unique per … Read more

Django migrations with multiple databases

You have to run migrate once for each database, specifying the target with –database. Each time it will consult your router to see which migrations to actually perform on that database. I’m guessing it was designed this way to favor explicitness over implicitness. For example, your workflow might require you to migrate the different databases … Read more

How to pass url parameter to reverse_lazy in Django urls.py

I wouldn’t do this directly in the urls.py, I’d instead use the class-based RedirectView to calculate the view to redirect to: from django.views.generic.base import RedirectView from django.core.urlresolvers import reverse_lazy class RedirectSomewhere(RedirectView): def get_redirect_url(self, param): return reverse_lazy(‘resource-view’, kwargs={‘param’: param}, current_app=’myapp’) Then, in your urls.py you can do this: urlpatterns = patterns(”, url(r’^coolresource/(?P<param>\d+)/$’, RedirectSomewhere.as_view()), )

Get value of another field in Field level Validation in DRF

No that is not possible. If you need to access more than one value you have to use the Object-level validation (see docs): class Keys_Serializer(serializers.Serializer): key_id = serializers.IntegerField(required=True) key_name = serializers.CharField(required=True) value_id = serializers.IntegerField(required=False) def validate(self, data): # here you can access all values key_id = data[‘key_id’] value_id = data[‘value_id’] # perform you validation if … Read more

tech