How to override the default value of a Model Field from an Abstract Base Class

You can actually do this as follows: class BaseMessage(models.Model): is_public = models.BooleanField(default=False) # some more fields… class Meta: abstract = True class Message(BaseMessage): # some fields… Message._meta.get_field(‘is_public’).default = True I have done this once or twice. It works, because the field on Message is a different instance than the field on BaseMessage. However, I doubt … Read more

Modify default queryset in django

You can do this with a custom model manager and override the get_queryset function to always filter canceled=False. class CustomManager(models.Manager): def get_queryset(self): return super().get_queryset().filter(canceled=False) class MyModel(models.Model): # Blah blah objects = CustomManager() Then when calling MyModel.objects.all() it will always exclude canceled objects. Here is a blog post I found helpful on the subject. http://www.b-list.org/weblog/2006/aug/18/django-tips-using-properties-models-and-managers/ EDIT: … Read more

Django store user image in model

You want to use the “upload_to” option on an ImageField #models.py import os def get_image_path(instance, filename): return os.path.join(‘photos’, str(instance.id), filename) class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) profile_image = ImageField(upload_to=get_image_path, blank=True, null=True) This is code directly from a project of mine. The uploaded image goes to /MEDIA_ROOT/photos/<user_id>/filename For what you want, just change the ‘photos’ string … Read more

Trigering post_save signal only after transaction has completed

I think the simplest way is to use transaction.on_commit(). Here’s an example using the models.Model subclass Photo that will only talk to Elasticsearch once the current transaction is over: from django.db import transaction from django.db.models.signals import post_save @receiver(post_save, sender=Photo) def save_photo(**kwargs): transaction.on_commit(lambda: talk_to_elasticsearch(kwargs[‘instance’])) Note that if the transaction.on_commit() gets executed while not in an active … Read more

How do I add a link from the Django admin page of one object to the admin page of a related object?

Use readonly_fields: class MyInline(admin.TabularInline): model = MyModel readonly_fields = [‘link’] def link(self, obj): url = reverse(…) return mark_safe(“<a href=”https://stackoverflow.com/questions/9919780/%s”>edit</a>” % url) # the following is necessary if ‘link’ method is also used in list_display link.allow_tags = True

Django error message displayed for unique fields

Thank you very much. email = models.EmailField(unique=True, error_messages={‘unique’:”This email has already been registered.”}) this worked very well now. If you want to customise error_messages like invalided, do it in forms.ModelForm email = forms.EmailField(error_messages={‘invalid’: ‘Your email address is incorrect’}) But unique message should be customised in model field, as ben mentioned email = models.EmailField(unique=True, error_messages={‘unique’:”This email … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)