django-models

How to set 2 attributes to primary key together in Django?

十年热恋 提交于 2021-02-09 11:01:11
问题 I have a model in Django: class Subject(models.Model): level = models.CharField(max_length=50) subject_name = models.CharField(max_length=50) teacher_name = models.ForeignKey(Teacher, on_delete=models.CASCADE) total_seats = models.IntegerField() subject_details = models.CharField(max_length=50) For the Subject table I want the level and the subject_name together to be primary keys. In fact, I dont want any other objects to have the same name and level. I know I can use unique_together but

How to set 2 attributes to primary key together in Django?

霸气de小男生 提交于 2021-02-09 11:00:56
问题 I have a model in Django: class Subject(models.Model): level = models.CharField(max_length=50) subject_name = models.CharField(max_length=50) teacher_name = models.ForeignKey(Teacher, on_delete=models.CASCADE) total_seats = models.IntegerField() subject_details = models.CharField(max_length=50) For the Subject table I want the level and the subject_name together to be primary keys. In fact, I dont want any other objects to have the same name and level. I know I can use unique_together but

Django point ImageField to an already existing image

梦想与她 提交于 2021-02-09 09:22:06
问题 I have a model which has an Image field: class Foo(models.Model): image = models.ImageField(upload_to = "bar", blank = True) I am downloading an image over the internet using urllib like this: urllib.urlretrieve(img_url, img_path) Now I want to point the Image field to this downloaded image saved as img_path. I have tried using Image.open() from PIL to first open the file and point to it and obj.image.save(File(open(file_path))) using django.core.files.File but they don't seem to work. Is

Django point ImageField to an already existing image

我的未来我决定 提交于 2021-02-09 09:19:55
问题 I have a model which has an Image field: class Foo(models.Model): image = models.ImageField(upload_to = "bar", blank = True) I am downloading an image over the internet using urllib like this: urllib.urlretrieve(img_url, img_path) Now I want to point the Image field to this downloaded image saved as img_path. I have tried using Image.open() from PIL to first open the file and point to it and obj.image.save(File(open(file_path))) using django.core.files.File but they don't seem to work. Is

how to add dynamic fields at run time in django

对着背影说爱祢 提交于 2021-02-08 15:05:55
问题 I have to add dynamic fields at run time in my django application,but I don't know the proper way how to add new fields at run time. I want to add the code which will generate the dynamic field and will update database too. I am using postgresql database. please help if anyone can. My "model.py" is simply like this: class Student(models.Model): name=models.CharField(max_length=100) school=models.CharField(max_length=100) created_at=models.DateField(auto_now_add=True) is_active=models

Adding custom attributes to django model field

偶尔善良 提交于 2021-02-08 13:34:10
问题 Can I add some arbitrary attributes to django model field? For example: charField = models.CharField('char field', max_length=1024, aaaaa=true) Can I add attribute aaaaa? Is this doable? 回答1: If you check CharField class CharField(Field): description = _("String (up to %(max_length)s)") def __init__(self, *args, **kwargs): super(CharField, self).__init__(*args, **kwargs) self.validators.append(validators.MaxLengthValidator(self.max_length)) It accepts **kwargs but also calls super and

Adding custom attributes to django model field

夙愿已清 提交于 2021-02-08 13:33:27
问题 Can I add some arbitrary attributes to django model field? For example: charField = models.CharField('char field', max_length=1024, aaaaa=true) Can I add attribute aaaaa? Is this doable? 回答1: If you check CharField class CharField(Field): description = _("String (up to %(max_length)s)") def __init__(self, *args, **kwargs): super(CharField, self).__init__(*args, **kwargs) self.validators.append(validators.MaxLengthValidator(self.max_length)) It accepts **kwargs but also calls super and

How to I create an expiration date in my django model?

戏子无情 提交于 2021-02-08 11:48:20
问题 How would I create an expiration date option for new posts that would only show when they are unexpired? 回答1: In your model you need something like this: expiration_date = models.DateTimeField() Then in your views you could access the database like this: def UnexpiredPosts(request): unexpired_posts = YourPostModelName.objects.filter(expiration_date__gt = datetime.now()) return render(request, "path_to_template", {'Posts': unexpired_posts}) The template: <ul> {% for i in Posts %} <li>{{ i }}<

How to set Collation in MySQL database with Django 2.* mysqlclient?

僤鯓⒐⒋嵵緔 提交于 2021-02-08 10:49:40
问题 I need to set default Collation for MySQL tables with Django 2.*, im using mysqlclient, my settings are: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '', 'USER': '', 'PASSWORD': '', 'HOST': 'localhost', 'PORT': '3306', 'OPTIONS': { 'charset': 'utf8mb4', }, } } 'charset': 'utf8mb4', This parameter seems don't work properly and tables in DB utf8. Although i want to manually set and tables Collation to utf8mb4_general_ci Will be appreciate for any clues. 回答1: 'default

How to set Collation in MySQL database with Django 2.* mysqlclient?

喜欢而已 提交于 2021-02-08 10:48:35
问题 I need to set default Collation for MySQL tables with Django 2.*, im using mysqlclient, my settings are: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '', 'USER': '', 'PASSWORD': '', 'HOST': 'localhost', 'PORT': '3306', 'OPTIONS': { 'charset': 'utf8mb4', }, } } 'charset': 'utf8mb4', This parameter seems don't work properly and tables in DB utf8. Although i want to manually set and tables Collation to utf8mb4_general_ci Will be appreciate for any clues. 回答1: 'default