Set two fields to the same value
Hello, how can I design a model such that two fields are set to the same value when a new object of the type defined by the model is created? Example: class Test(models.Model): start_time = models.DateTimeField() end_time = models.DateTimeField() time = start_time Users may create Test objects in order to run their own tests. A test always starts at some date time and it always ends at some date time. Each test has a time which is increased until it equals the end time. When a user creates a new test (and before he uses it) the field time should have the same value as the field start_time. The code above will produce a database error. Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/ekl9tv3zHTIJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: calculate default values for fields based on the content of other fields
On 5 Apr., 10:40, Xavier Ordoquywrote: > Hi, > > I'm not sure I clearly understood your issue. > Do you want some choice fields to limit their choices according to another > choice field value ? No. I want to use prepopulated editable char fields. The content of those fields should be calculated using the content of another char field. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: calculate default values for fields based on the content of other fields
> However, I don't think you need to do this at all. I would say you > would only populate the other fields if they are all empty, and the > base is not empty. If the other fields are not empty, either it has > already been pre-populated, or it has been filled in manually, and you > should not change it. Seems pretty straightforward! I think this is not an option: In the majority of cases, correcting a form will mean to delete it. Example: The program might generate the comparative form "magicaler" and the superlative form "magicalest" for the English adjective "magical". The administrator's job is to delete these forms. Of course, I might require every field to have a content and use some special symbol, such as '-' to denote that a field is empty. But I think this would unnecessarily complicate the coders' work. The fewer rules and conventions the coder needs to know, the better. And it wouldn't help either, because sometimes - for example in the case of English adjectives of a certain length - the program would assign this symbol or the empty string itself. > save() happens before the data is saved to the database (well, its the > last things that happens in Model.save()). So you can pull the > original data from the database, and compare it to the one you are > being asked to save. I don't quite understand you. Where exactly is the data I am "being asked to save"? The following function might be an approximation to what you suggest: def save(self, *args, **kwargs): #1 tmp = self.attr_pos_str_masc_nom_sing #2 if tmp != self.default_attr_pos_str_masc_nom_sing() #3 pass #4 else: #5 self.attr_pos_str_masc_nom_sing = self.default_attr_pos_str_masc_nom_sing() #6 super(GermanAdjective, self).save(*args, **kwargs) #7 (I hope this will keep its indentation.) In line 2, I look up the value of the field attr_pos_str_masc_nom_sing. In line 3, I check if the current value of this field differs from the default value for this field. If so, I assume that the field has been edited and do nothing. Yet, when an adjective is created, this field is empty, so the test is positive and nothing is done. Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: calculate default values for fields based on the content of other fields
> You could override Model.save() on the model you wish to calculate > fields for. This could populate the fields if they are not already > populated and the instance has a base adjective. > I had thought of this possibilty, too. I would prefer it, because it is the simplest one. But I don't understand how to find out in the save() function if the admin has modified the field. If I don't know this, the field will get the default value every time an administrator saves the word. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
calculate default values for fields based on the content of other fields
Hello, is there a way to calculate default values for certain fields based on the content of other fields? I am implementing a dictionary. Admins are supposed to add words of certain languages and parts-of-speech. Some words of some part-of-speech and language are, on one hand, morphologically very regular, but, one the other hand, come with a huge number of morphological forms. This is true for German adjectives: They usually have 183 morphological forms (if form-meaning pairs are counted). On the other hand, there are very few irregular adjectives. The most user-friendly way for admins to code German adjectives would be this: The admin adds a new adjective and types in the adjective's base form: base form: |gut| Then he clicks some button or saves the adjective or, ideally, does nothing, and the program fills in the 183 fields below, using the base form and assuming that the adjective is regular: attributive positive strong masculine nominative singular: |guter| attributive positive strong masculine genitive singular: |guten| attributive positive strong masculine dative singular: |gutem| attributive positive strong masculine accusative singular: |guten| ... attributive comparative strong masculine nominative singular: | guterer| ... The admin now has the possibility to correct the wrong forms before saving the adjective. This way, in the vast majority of cases all the coding is done completely automatically. And for the very few irregular adjectives, the admin only has to modify those forms that have been guessed wrongly. Any ideas how to realize this? Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
field of tuples
Hello, how can I create a field that contains tuples of entities? An example: A model "Candidate" (someone who applies for a job) might have a field "languages" which is a list of tuples consisting of a language and a proficiency level. Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
field name hiding workaround
Hello, I have got a class Lexeme which I would like to define as follows: class Lexeme(models.Model): class Meta: unique_together = ((u"entity", u"language"),) language = models.ForeignKey(Language) # ... I would like to have different types of lexemes, each of which has another field type as "entity". Like so: class CommodityLexeme(Lexeme): class Meta: verbose_name_plural = "Commodities" entity = models.ForeignKey(Commodity) class CountryLexeme(Lexeme): class Meta: verbose_name_plural = "Countries" entity = models.ForeignKey(Country) I know this doesn't work, because field name hiding is not permitted. (See https://docs.djangoproject.com/en/1.1/topics/db/models/#field-name-hiding-is-not-permitted). But what would be the most elegant workaround? I suppose someone has had a similar problem before. Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: document admin interface
On 16 Jan., 16:36, Sébastien Billionwrote: > Hi, > > Why don't use help_text argument in your models or formAdmin? Thank you. That was exactly what I was looking for. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
document admin interface
Hello, how can I document the admin interface. I do not mean the stuff that can be achieved with the admin documentation generator. I would like to add documentation to those pages that admins see when they create, delete or modify an object. I.e. I want to add explanations about the object being manipulated. Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
sorting of foreign key elements
Hello, is there a way to change the order in which foreign key elements are displayed in the admin interface? I have got a model Currency that has a field currency_id: currency_id = models.ForeignKey(CurrencyID) ("Currencies" are language-dependent ways of referring to currencies, "CurrencyIDs" are language-independent objects.) Although my CurrencyID objects are sorted alphabetically (Afghan Afghani, Albanian Lek, Algerian Dinar, ...) they appear in reversed order in the currency_id slot in the admin interface of Currency objects (Zimbabwe Dollar, Zambian Kwacha, Yemeni Rial ...). Can I change this? If so, how? Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: clean function for field that may be blank
> https://docs.djangoproject.com/en/1.3/ref/models/fields/#blankhttps://docs.djangoproject.com/en/1.3/ref/models/fields/#null > > The confusion comes about because charfields do not require null=True, > as empty or missing strings are stored as the empty string rather than > as a null string. > The code works with null=True. Thanks for pointing this out. Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: clean function for field that may be blank
I forgot to say: (1) I deleted the database completely and created it again. (2) I ran manage.py syncdb However, with this code def clean_date_of_birth(self): date_of_birth = self.cleaned_data.get('date_of_birth') if date_of_birth: if date_of_birth.year < 1890: raise forms.ValidationError(u"It is very improbable...") return date_of_birth I still get the database problem. Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: clean function for field that may be blank
> def clean_date_of_birth(self): > date_of_birth = self.cleaned_data.get('date_of_birth') > if date_of_birth: > if date_of_birth.year < 1890: > raise forms.ValidationError(u"It is very improbable...") > return date_of_birth > I had already tried this. It will produce a database error: According to the postgresql server date_of_birth must not be null: FEHLER: NULL-Wert in Spalte »date_of_birth« verletzt Not-Null- Constraint (Unfortunately, our postgresql server produces German error messages and our system administrator will not change this. I don't know the exact wording of this error message in English.) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
clean function for field that may be blank
Hello, I have got a class Person: class Person(models.Model): given_names = models.CharField(max_length=200) family_names = models.CharField(max_length=200) date_of_birth = models.DateField(blank=True) and a class PersonForm: class PersonForm(forms.ModelForm): def clean_date_of_birth(self): try: date_of_birth = self.cleaned_data['date_of_birth'] if date_of_birth.year < 1890: raise forms.ValidationError(u"It is very improbable for a person to be this old.") return date_of_birth except KeyError: pass The code works as expected if I leave out "blank=True" in the field date_of_birth of the class Person. But it doesn't work with blank=True. How does clean_date_of_birth have to look like, if date_of_birth is optional? Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Set of choices of which several can be chosen without using a ManyToManyField
On 1 Nov., 18:54, Mark Furbeewrote: > As alluded to previously, the most "straightforward way to use a set of > choices of which several can be chosen" IS to use a ManyToManyField. The > syntax is slightly different, but ManyToManyFields are really easy to use > with Django. Do not reinvent the wheel in this case. I am not against ManyToManyFields. Yet, in certain cases (which are actually more complicated than the (invented) Candidate/language example), I would like to not create certain data as objects that can be seen, modified, created and deleted by administrators. Administrators should only see those data which they are supposed to manipulate. That is why I wanted to have the possibility to "hardwire" certain data in my source code in the same way as data is hardwired in the source code with the "choices=" solution that can be used with CharField. I now use ManyToManyFields and hide certain data from administrators by simply not importing them into admin.py. What I don't like about this solution is that this data still is in the database and not in my source code. In my view, it should be part of the source code, because it is part of the program logic: Any instance of my program is supposed to use these data, independently of any other set of data it might use. Much as any instance of a program that uses the notion of gender should have the genders "male" and "female". Administrators should not have to or be able to manipulate gender objects. Thanks to all who participated in this thread. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Set of choices of which several can be chosen without using a ManyToManyField
> You are confusing model fields with form fields. MultipleChoiceField > is a form field, not a model field. I wasn't aware of the existence of MultipleChoiceFields. The idea of the above code was to express that I wanted to use this code class Candidate(models.Model): programming_languages = models.CharField(max_length=50, choices=( (u'Python)', u'Python'), (u'C++', u'C++'), (u'Java', u'Java'), # ... ), blank=True) with the only exception that, in the admin interface, several choices are possible when one creates a new candidate object. I.e. I want admins to be able to create a candidate that knows, say Python *and* C+ + by choosing both of these languages during the creation of the object. I used the string "MultipleChoiceField" as a dummy for whatever should be used instead. Jaroslav > If you want a field that will be represented by a MultipleChoiceField > in model, you simply need to define 'choices' on a field class. > > https://docs.djangoproject.com/en/1.3/ref/models/fields/#choices > > Cheers > > Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Set of choices of which several can be chosen without using a ManyToManyField
Hello, what is the most straightforward way to use a set of choices of which several can be chosen without using a ManyToManyField? Using a ManyToManyField would make my program unnecessarily complicated. Example: class Candidate(models.Model): programming_languages = models.MultipleChoiceField(max_length=50, choices=( # replace MultipleChoiceField with existing possibility (u'Python)', u'Python'), (u'C++', u'C++'), (u'Java', u'Java'), # ... ), blank=True) Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: create object instance with ManyToManyField
Hello Leonardo, thanks your your answer. > n = Company(name=my_name, country=my_country, isin=my_isin) > n.save() > n.indices.add(my_indices) This causes that the company object has all indices in my_indices (such as Dow Jones S 100, Dax, ...) as *choices*. I.e. someone who manipulates these objects via the admin interface is now able to choose one or several of them and save the object again. What I want to do is choose one or several of the indices in my program and then save the object. Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
create object instance with ManyToManyField
Hello, how can I create an object instance with a ManyToManyField. Everything works fine via the admin interface. But I don't understand how to do it in Python. Example: I have this model: class Company(models.Model): name = models.CharField(max_length=200, unique=True) country = models.ForeignKey(Country) isin = models.CharField(max_length=12, blank=True) indices = models.ManyToManyField(Index, blank=True) def the_indices(self): ind = [] for index in self.indices.all(): ind.append(index.name) return ', '.join(ind) the_indices.short_description = u'Indices' def __unicode__(self): return self.name This will work: n = Company(name=my_name, country=my_country, isin=my_isin) n.save() This will not work: n = Company(name=my_name, country=my_country, isin=my_isin, indices=my_indices) n.save() Although I make sure that my_indices is a list of existing index objects. What do I have to do? Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: store superuser permamently
On 3 Feb., 15:19, Ivo Brodienwrote: > > What do I have to do to avoid having to define a superuser each time? > > you can dump the data of the auth app and than load it again. > > s.th. like this: > > ./manage.py dumpdata --indent=4 auth > fixtures/auth.json > ./manage.py dumpdata --indent=4 sessions > fixtures/sessions.json > > you can do a manage.py syncdb —noinput so you will not be asked, if you want > a superuser or not. > > If you dump the sessions too you will stay logged in. Thank you. That is exactly what I wanted. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
store superuser permamently
Hello, I often use manage.py dumpdata --indent 2 > initial_data.json and then manipulate initial_data.json. When I remove my database file and run manage.py syncdb almost all data from inital_data.json are installed. (Many errors only go away when the database file is removed.) But manage.py asks me each time: You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): And then I have to type "yes", type the superuser's name, email adress ... When you say "no", the data is correctly installed, but you can't log in to the admin interface. (I thought a system without a superuser would not be password protected. What point is there in being able to not define a superuser, if the system cannot be used without one?) What do I have to do to avoid having to define a superuser each time? Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: abstract base class and overwriting save()
> The whole point of subclassing is that subclasses inherit models from the > superclass. That's how the model save() method is definted in the first > place, if you don't override it. What exactly are you having problems with? It seems that my problems were due to another mistake of mine. I was thinking in a too complicated way. You are right: nothing has to be done, just inherit. Thank you. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
abstract base class and overwriting save()
Hi, if I have got several models that have identical code, except for their save method, how can I use an abstract base class for them? Will I have to overwrite save() for each model? Or is there a generic way to define save(), i.e. a way that I have to use only once -- in the abstract base class? class BMW(models.Model): ... save def save(self, *args, **kwargs): do_something() super(BMW, self).save(*args, **kwargs) class Fiat(models.Model): ... save def save(self, *args, **kwargs): do_something() super(Fiat, self).save(*args, **kwargs) Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: forbid clones
On 26 Jan., 13:29, Chris Matthewswrote: > Have a look at unique_together: > Django | Model Meta options | Django > documentationhttp://docs.djangoproject.com/en/dev/ref/models/options/ > > Jump to unique_togetherý: Options.unique_together¶. Sets of field names that, > ... For convenience, unique_together can be a single list when dealing ... Great, thank you. That is exactly what I was looking for. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: forbid clones
> > http://docs.djangoproject.com/en/1.2/ref/models/fields/#unique Although this does help, it leaves one question open: How can we forbid only such pairs of objects that have the same value in all of their attributes. Example: This should be allowed: car1: manufacturer = "foo", name = "bar" car2: manufacturer = "foo", name = "baz" This should not be allowed: car1: manufacturer = "foo", name = "bar" car2: manufacturer = "foo", name = "bar" -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: forbid clones
> http://docs.djangoproject.com/en/1.2/ref/models/fields/#unique > Wow, thanks. In this group answers almost come before one is able to submit the posting. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
forbid clones
Hi, how can I forbid that clones of a certain type are stored by an admin? Example: Suppose there are cars in the database and cars have as only attribute a name. How can I prevent that two cars that have the same name are stored? Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: method of method
Thanks to all of you. Now I understand this better. Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: method of method
On 25 Jan., 12:36, Euan Goddardwrote: > I agree with the previous poster - the title is misleading as the word > "method" is incorrect in both places. O.k. sorry. Should have been "attribute of attribute", right? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: method of method
>> What happens ? I get this error message when I run manage.py syncdb: AttributeError: 'ForeignKey' object has no attribute 'producer' >> And what did you expect to happen ? I want the 'country_of_origin' of cars to be the string that is stored in their producer's 'country_of_origin': class CarProducer(models.Model): country_of_origin = models.CharField(max_length=200) class Car(models.Model): producer = models.ForeignKey(CarProducer) country_of_origin = producer.country_of_origin -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
method of method
Hi, can I use methods of methods in Django? Like so: class Car(models.Model): producer = models.ForeignKey(CarProducer) country_of_origin = producer.country_of_origin This doesn't seem to work. Is there a special syntax for this? Jaroslav -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.