django-models

Change choices attribute of model's field

淺唱寂寞╮ 提交于 2020-01-03 04:46:10
问题 This question is similar to Set model field choices attribute at run time? However, my problem is that I want to change the default value of the choices attribute, at class level. I have this class Project(models.Model): name = models.CharField(...) @staticmethod def getAllProjectsTuple(): return tuple([(p.id, p.name) for p in Project.objects.all()]) class Record(models.Model): project = models.ForeignKey( Project, verbose_name='Project', help_text='Project name', blank=True, null=True, on

Search fields in multiple models in Django

梦想与她 提交于 2020-01-03 02:55:08
问题 Suppose, A model named Education contains the fields degree and field , and other model Resume contains the fields skill and role . A third model is Candidates and has a foreign key relation with the above models. I want the user to search candidates by their skill , role , degree or field . For example: if a query string like {'java','developer','MS','IT'} is passed, Django should show the all the candidates matching any one of the values in the query string. 回答1: If you're doing this with

Django: Adding objects to a related set without saving to DB

懵懂的女人 提交于 2020-01-03 02:00:12
问题 I'm trying to write an internal API in my application without necessarily coupling it with the database. class Product(models.Model): name=models.CharField(max_length=4000) price=models.IntegerField(default=-1) currency=models.CharField(max_length=3, default='INR') class Image(models.Model): # NOTE -- Have changed the table name to products_images width=models.IntegerField(default=-1) height=models.IntegerField(default=-1) url=models.URLField(max_length=1000, verify_exists=False) product

Django api calculate count of responses

做~自己de王妃 提交于 2020-01-03 01:09:14
问题 I am trying to do a survey application in django. My model is as follows: class mymodel(models.Model): resptype = models.ForeignKey(Response) ques = models.ForeignKey(Question) response = models.CharField(max_length=5, blank=True) Here i am using rest framework to send data to my front end. Right now i have my api defined as follows: class mymodelList(APIView): def get(self, request, format=None): surveydata = mymodel.objects.all() serialized_surveydata = mymodelSerializer(surveydata, many

Relationships in Django Admin

夙愿已清 提交于 2020-01-02 22:33:09
问题 I get really confused with many-to-many database relationships, so can some one please clarify how I would achieve this? I need a table of "Tags" (as in tag words) and a table for "Entries", such at many "Entries" could correspond to many Tag words. Right now I have my models like this: # models.py class Tags(models.Model): tag = models.CharField(max_length=255) entry = models.ManyToManyField(Entry) class Entry(models.Model): entry = models.CharField(max_length=255) description = models

Django, Import tables as models

匆匆过客 提交于 2020-01-02 21:50:32
问题 I would like to know if it's possible to use django over existing db tables that defines the models. Instead of defining models in order to create db tables 回答1: Did you look in the documentation? 回答2: i think you are looking for inspectdb 来源: https://stackoverflow.com/questions/1789191/django-import-tables-as-models

Django: Model clean method called before form clean

元气小坏坏 提交于 2020-01-02 13:19:32
问题 I don't understand, why is my model clean method called before full form validation. I have required fields in my form. If I don't fill them, I don't get form errors, instead of, the model clean method is called (so I suppose it's because save is called). It crashes in models clean() method: if self.date_from > self.date_to can't compare datetime.date to NoneType Because I didn't filled date_to field. I think that form should have handle it and raise ValidationError and models save() shouldn

Django: Model clean method called before form clean

无人久伴 提交于 2020-01-02 13:19:15
问题 I don't understand, why is my model clean method called before full form validation. I have required fields in my form. If I don't fill them, I don't get form errors, instead of, the model clean method is called (so I suppose it's because save is called). It crashes in models clean() method: if self.date_from > self.date_to can't compare datetime.date to NoneType Because I didn't filled date_to field. I think that form should have handle it and raise ValidationError and models save() shouldn

How can I set/provide a default value while django migration?

你。 提交于 2020-01-02 10:30:21
问题 Scenario : I have a model, Customer class Customer(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() company = models.CharField(max_length=100) and now I updated the company attribute witha ForeignKey relationship as below, class Company(models.Model): name = models.CharField(max_length=100) location = models.CharField(max_length=100) class Customer(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() company = models.ForeignKey

How to specify Select and RadioSelect in a ModelForm?

巧了我就是萌 提交于 2020-01-02 10:18:28
问题 I am converting a survey from a Form to a ModelForm in Django 1.6.2. Can anyone tell me what is the equal of forms.ChoiceField(widget=forms.Select(), and forms.ChoiceField(widget=forms.RadioSelect() using ModelForm ? I have tried widget=models.Select() or widget=models.RadioSelect() but it keeps giving the error AttributeError: 'module' object has no attribute 'Select' AttributeError: 'module' object has no attribute 'RadioSelect' Old Code forms.py class SurveyFormB(forms.Form): #Do you own a