django-models

Django model field validation without a custom form

China☆狼群 提交于 2020-01-01 04:32:17
问题 I am using a django DateField in my model. class CalWeek(models.Model): start_monday = models.DateField(verbose_name="Start monday") I have a custom validation method that is specific to my modelField: I want to make sure that it is actually a Monday. I currently have no reason to use a custom ModelForm in the admin--the one Django generates is just fine. Creating a custom form class just so i can utilize the clean_start_monday(self) 1 sugar that django Form classes provide seems like a lot

Iterating through model fields - Django

百般思念 提交于 2020-01-01 04:19:09
问题 I'm trying to iterate through fields as they are written down within my model : currently I'm using this: def attrs(self): for attr, value in self.__dict__.iteritems(): yield attr, value but the order seems pretty much random :( Any ideas? 回答1: The _meta attribute on Model classes and instances is a django.db.models.options.Options which provides access to all sorts of useful information about the Model in question. For fields, it will give you them in the order they were created (i.e. the

How to deal with “SubfieldBase has been deprecated. Use Field.from_db_value instead.”

╄→гoц情女王★ 提交于 2020-01-01 04:11:07
问题 On upgrade to Django 1.9, I now get the warning RemovedInDjango110Warning: SubfieldBase has been deprecated. Use Field.from_db_value instead. I see where the problem arises. I have some custom field definitions, and in them I have __metaclass__ = models.SubfieldBase . For example, class DurationField(models.FloatField): __metaclass__ = models.SubfieldBase def __init__(self, *args, **kwargs): ... If the __metaclass__ statement is deprecated, what am I supposed to replace it with exactly? Do I

Django - Excluding some fields in Inline Admin Interface

坚强是说给别人听的谎言 提交于 2020-01-01 03:58:29
问题 In Django admin interface, Is to possible to exclude some of the fields in Inline? 回答1: with exclude you can do it ex: class Book(models.Model): author = models.ForeignKey(Author) title = models.CharField(max_length=100) short_description = models.CharField(max_length=200) class BookInline(admin.TabularInline): model = Book exclude = ['short_description'] 来源: https://stackoverflow.com/questions/4187291/django-excluding-some-fields-in-inline-admin-interface

What's the best way to create a 'history' type model in django?

帅比萌擦擦* 提交于 2020-01-01 03:57:22
问题 I'd like to create a feature for my Django app similar to Django admin's 'Recent Actions', in order to store history information on my other models. For example say I have two models called Book and Author. I want to have a third model that stores information such as what action was performed on a given object in a model (add, modify, delete, etc.) by who and when. Who, when and the action are easy, I'm just unsure about how to store information regarding what object the action was performed

Django model one foreign key to many tables

与世无争的帅哥 提交于 2020-01-01 03:18:09
问题 So I have a question I was thinking of creating a single table that has a foreign key to several other tables, and using another field "type" to say what table the key should belong to. class Status(Models.model): request = models.ForeignKey("Request1", "Request2", "Request3") request_type = models.IntegerField() ...Some status related data class Request1(Models.model): ...Some data class Request2(Models.model): ...Some other data Class Request3(Models.model): ...different data My question is

Django and models with multiple foreign keys

可紊 提交于 2020-01-01 02:44:15
问题 I am new to Django and I've been impressed so far by its capabilities. I am playing with more complex models and I am have problem to use them properly. Using Django 1.3, I am trying to write a summary page which would present the three models below with the following structure. In other words, a list of trips with their destinations and activities. Trip 1 Destination 1 Destination 2 Activity 1 Trip 2 Destination 1 Activity 2 Models Trip <-> TripDestination <-> Destination (a trip can have

Django: Order a model by a many-to-many field

Deadly 提交于 2020-01-01 02:19:19
问题 I am writing a Django application that has a model for People, and I have hit a snag. I am assigning Role objects to people using a Many-To-Many relationship - where Roles have a name and a weight. I wish to order my list of people by their heaviest role's weight. If I do People.objects.order_by('-roles__weight'), then I get duplicates when people have multiple roles assigned to them. My initial idea was to add a denormalized field called heaviest-role-weight - and sort by that. This could

Query when parameter is none django

蓝咒 提交于 2020-01-01 02:17:09
问题 I want to make a query, something like Model.objects.filter(x=x).filter(y=y).filter(z=z) ... but there are some cases when for example y is None. This literally searches the database for null values in the y column -- is there a nifty way to essentially disregard that query parameter if it is none, i.e. return the queryset Model.objects.filter(x=x).filter(z=z)? 回答1: I do not know, if I get your question, but Model.objects.filter(x=x, y__isnull = False, z=z) gives you the queryset, where the y

django - Get the set of objects from Many To One relationship

ぐ巨炮叔叔 提交于 2020-01-01 02:07:50
问题 Please have a look at these models: class Album(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) class Photo(models.Model): album = models.ForeignKey(Album, default=3) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) How do I get the the set of photos for a particular album??? And how to get the