django-models

How to migrate from custom primary key to default id [duplicate]

可紊 提交于 2020-08-22 09:34:11
问题 This question already has answers here : What is the best approach to change primary keys in an existing Django app? (8 answers) Closed 23 days ago . I have created a model with email address as custom primary key as follows: email = models.EmailField(max_length=255, primary_key=True,) Now I realized that this is not a good idea in my case and I would like to go back to the automatically generated id field as primary key. How to do this? I tried this in different ways but all failed. I am

How to migrate from custom primary key to default id [duplicate]

橙三吉。 提交于 2020-08-22 09:32:51
问题 This question already has answers here : What is the best approach to change primary keys in an existing Django app? (8 answers) Closed 23 days ago . I have created a model with email address as custom primary key as follows: email = models.EmailField(max_length=255, primary_key=True,) Now I realized that this is not a good idea in my case and I would like to go back to the automatically generated id field as primary key. How to do this? I tried this in different ways but all failed. I am

Django Model Fields Indexing

江枫思渺然 提交于 2020-08-21 04:59:28
问题 I only know that indexing is helpful and it queries faster. What is the difference between following two? 1. class Meta: indexes = [ models.Index(fields=['last_name', 'first_name',]), models.Index(fields=['-date_of_birth',]), ] 2. class Meta: indexes = [ models.Index(fields=['first_name',]), models.Index(fields=['last_name',]), models.Index(fields=['-date_of_birth',]), ] 回答1: Example 1: The first example creates a single index on the last_name and first_name field. indexes = [ models.Index

How to create Django like button for anonymous users?

自作多情 提交于 2020-08-19 17:20:07
问题 I am using Django and my website has no user profiles so all are anonymous. I want to implement a 'like' system. How do I restrict a user to like only once. Thanks. 回答1: If you don't have any way of identifying your users then your best bet is to store this info in a browser cookie or HTML5 local storage . (I don't advise using flash cookies since there is a long debate about them and they are harder to implement) 回答2: You can't 100% restrict multiple votes, but you can make it very difficult

How to create Django like button for anonymous users?

耗尽温柔 提交于 2020-08-19 17:19:37
问题 I am using Django and my website has no user profiles so all are anonymous. I want to implement a 'like' system. How do I restrict a user to like only once. Thanks. 回答1: If you don't have any way of identifying your users then your best bet is to store this info in a browser cookie or HTML5 local storage . (I don't advise using flash cookies since there is a long debate about them and they are harder to implement) 回答2: You can't 100% restrict multiple votes, but you can make it very difficult

How to create Django like button for anonymous users?

不打扰是莪最后的温柔 提交于 2020-08-19 17:18:38
问题 I am using Django and my website has no user profiles so all are anonymous. I want to implement a 'like' system. How do I restrict a user to like only once. Thanks. 回答1: If you don't have any way of identifying your users then your best bet is to store this info in a browser cookie or HTML5 local storage . (I don't advise using flash cookies since there is a long debate about them and they are harder to implement) 回答2: You can't 100% restrict multiple votes, but you can make it very difficult

Django Query __isnull=True or = None

纵然是瞬间 提交于 2020-07-31 16:31:29
问题 this is a simple question. I'd like to know if it is the same to write: queryset = Model.objects.filter(field=None) than: queryset = Model.objects.filter(field__isnull=True) I'm using django 1.8 回答1: They are equal: >>> str(Person.objects.filter(age__isnull=True).query) == str(Person.objects.filter(age=None).query) True >>> print(Person.objects.filter(age=None).query) SELECT "person_person"."id", "person_person"."name", "person_person"."yes", "person_person"."age" FROM "person_person" WHERE

What is best practice for dealing with blank ('') string values on non-nullable model fields?

▼魔方 西西 提交于 2020-07-23 07:40:28
问题 In Django/postgres if you have a non-nullable field, it's still possible to store a blank str value '' for certain field types (e.g. CharField , TextField ). Note I am not referring to blank=False , which simply determines if the field is rendered with required in a modelForm. I mean a literal blank string value. For example, consider this simple model that has null=False on a CharField : class MyModel(models.Model): title = models.CharField('title', null=False) The following throws an

What is best practice for dealing with blank ('') string values on non-nullable model fields?

左心房为你撑大大i 提交于 2020-07-23 07:39:19
问题 In Django/postgres if you have a non-nullable field, it's still possible to store a blank str value '' for certain field types (e.g. CharField , TextField ). Note I am not referring to blank=False , which simply determines if the field is rendered with required in a modelForm. I mean a literal blank string value. For example, consider this simple model that has null=False on a CharField : class MyModel(models.Model): title = models.CharField('title', null=False) The following throws an

duplicate Key violation in Django insert doing after Django update

[亡魂溺海] 提交于 2020-07-23 06:10:24
问题 first i update my model instance, after that i tried to insert a new data but showing "IntegrityError('duplicate key value violates unique constraint "RFIDActivation_ActivationId_key"\nDETAIL: Key ("ActivationId")=(6de9ed9a) already exists.\n',)" Models.py class RFIDActivation(models.Model): RFIDActivationId = models.AutoField(primary_key=True, db_column='RFIDActivationId') Device = models.ForeignKey(Device, on_delete=models.CASCADE, db_column='DeviceId') Employee = models.ForeignKey(Employee