Suppose I have a model below :
class Post(MainProcess, TimeStampedModel, SoftDeletionModel, models.Model):
"""Post model."""
slug = models.SlugField(default=uuid.uuid4(), unique=True, max_length=100)
uuid = models.UUIDField(unique=True, max_length=500,
default=uuid.uuid4,
editable=False,
db_index=True, blank=False, null=False)
title = models.CharField(_('Title'), max_length=100, blank=False,
null=False)
image = models.ImageField(_('Image'), upload_to='blog_images', null=True,
max_length=900, blank=True)
I wanted to override the image field values without using the save() function, what is the best approach here which will be efficient.
Related
I'm trying to save unique name in the database but the problem I can save the same with different letters, for example I can save (IT, it, iT, It) I don't want to save it like that.
Model:
class Service(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=127, unique=True, null=False, blank=False) # that field
is_active = models.BooleanField(default=True)
is_deleted = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(
"accounts.User",
on_delete=SET_NULL,
blank=False,
null=True,
related_name="service_created_by",
)
def __str__(self):
return f"{self.name}"
A very simple solution:
class Service(models.Model):
name = models.CharField(max_length=50, unique=True)
....
def clean(self):
self.name = self.name.capitalize()
this one helped me
class Service(models.Model):
name = models.CharField(max_length=50, unique=True, null=False, blank=False)
....
class Meta:
constraints = [
models.UniqueConstraint(Lower("name"), name="unique_name"),
]
def clean(self):
self.name = self.name.capitalize()
I have a ForeignKey field in my model and in Django admin and Django forms` when I display that field or in Django admin, I get all the field that in that model However, I only want to display selected fields in that dropdown, for example
class Area(models.Model):
area_type_options = (('cc', 'Cost Center'), ('pc', 'Profit Center'),)
name = models.CharField(max_length=100)
area_type = models.CharField(max_length=100, choices=area_type_options)
class Item(models.Model):
name = models.CharField(max_length=150, null=True, unique=True)
profit_center = models.ForeignKey(Area, null=True, blank=True, on_delete=models.CASCADE, related_name='profit_center')
cost_center = models.ForeignKey(Area, null=True, blank=True, on_delete=models.CASCADE, related_name='cost_center')
I get to see all the records in the cost_center and all the records in the profit_center however I only want to see where area_type is cc to cost_center and where area type pc to profit_center
Please Help
You can use ForeignKey.limit_choices_to [Django docs] to do this:
class Item(models.Model):
name = models.CharField(max_length=150, null=True, unique=True)
profit_center = models.ForeignKey(Area, null=True, blank=True, on_delete=models.CASCADE, related_name='profit_center', limit_choices_to={'area_type': 'pc'})
cost_center = models.ForeignKey(Area, null=True, blank=True, on_delete=models.CASCADE, related_name='cost_center', limit_choices_to={'area_type': 'cc')
I have a model for my user and another model for music style. In my model of musical styles, I need to put the image under a name other than the original name.
The new name I would like is the 'email of the user who is saving' + '_' + 'milliseconds'.
Staying in this format:
'test#gmail.com_1621196336'
Model User:
class CustomUser(AbstractUser):
username = None
id = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False, null=False)
email = models.EmailField('E-mail', unique=True, null=False)
Model Music Styles:
class MusicStyle(models.Model):
id = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False, null=False)
name = models.CharField(max_length=150, null=False, blank=False)
image_link = models.FileField(upload_to='musics/thumbnail_images/##CUSTOM_NAME##')
Musical style is in a different model from the user model.
How to do this?
i didnt understand what the field with milliseconde is or what is supposed to do so i replaced it with with email and music_style name, here is how you can name your images, you can adjust it as you want
def upload_location(instance, filename):
filebase, extension = filename.split('.')
return 'musics/thumbnail_images/%s_%s.%s' % (instance.user.email,instance.name, extension)
class MusicStyle(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
name = models.CharField(max_length=150, null=False, blank=False)
image_link = models.FileField(upload_to=upload_location)
If you want to set custom filename for your files you can do this:
def image_upload_to(instance, filename):
# You can access to your model fields with instance for example: instance.name
return f"musics/thumbnail_images/{filename}"
class MusicStyle(models.Model):
id = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False, null=False)
name = models.CharField(max_length=150, null=False, blank=False)
image_link = models.FileField(upload_to=image_upload_to)
i want create a custom field with AutoField
description:
Code generated by the system is increasing gradually, the code consists of 5 digits, there are no 2 identical codes. For example: 00001, 00002
this is my code :
class Supplier(models.Model):
code = models.AutoField(primary_key=True, max_length=5, blank=False, null=False)
name = models.CharField(max_length=200, blank=False, null=False)
phone = models.CharField(max_length=11, unique=True, validators=[len_phone])
email = models.EmailField(blank=True, null=True, unique=True)
address = models.CharField(max_length=200, blank=False, null=False)
I have created a role model for Employee so that employee will be assigned to control
the overall app based on his/her role. I mean if the role of employee is given can_create_only, then the employee should be able to create inventory, orders, items etc and if employee is given can_create_edit_and_delete, then the employee would be like one of the admin and etc. For this I have designed the model as below but I want to know what is the best way to handle such and why?
Should I go with middleware or decorator way? Can anyone give me an example, please?
class Role(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
class Meta:
verbose_name = 'Role'
verbose_name_plural = 'Roles'
class Employee(models.Model):
office = models.ForeignKey(
OfficeSetup, blank=False, null=False, on_delete=models.CASCADE)
name = models.CharField(max_length=150, blank=False, null=False)
designation = models.ForeignKey(Designation, blank=False, null=False)
section = models.ForeignKey(DepartmentSetup, blank=True, null=True)
phone_number = models.CharField(max_length=150, blank=True, null=True)
mobile_number = models.CharField(max_length=150, blank=True, null=True)
email = models.EmailField(max_length=150, blank=False, null=False)
gender = models.CharField(
max_length=4, choices=GENDER, blank=True, null=True)
role = models.ForeignKey(Role, blank=True, null=True)
username = models.CharField(max_length=100, blank=False, null=False)
password = models.CharField(max_length=100, blank=False, null=False)
avatar = models.ImageField(
null=True, blank=True, upload_to=upload_employee_image_path)
class Meta:
verbose_name = 'Employee'
verbose_name_plural = 'Employees'
def __str__(self):
return self.name
When creating an employee by admin, the username, password and email, the admin provides will create a new user instance along with the employee
Django comes with Groups and permissions which provides all most everything you are looking for.
This may help you - How do I use Django groups and permissions?
Django documentation - https://docs.djangoproject.com/en/1.11/topics/auth/