django-models

Django - Show BooleanField in a formset as one group of radio buttons

為{幸葍}努か 提交于 2019-12-30 06:37:43
问题 I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) def primary_phone(self): return self.phone_set.get(primary=True) class Phone(models.Model): profile = models.ForeignKey(Profile) type = models.CharField(choices=PHONE_TYPES, max_length=16) number = models.CharField(max_length=32) primary = models.BooleanField(default=False) def save(self, force_insert=False, force_update=False, using=None): if self.primary: # clear the primary attribute of

How to filter for multiple ids from a query param on a GET request with django rest framework?

拥有回忆 提交于 2019-12-30 04:34:11
问题 I'm trying to make a web app API. I want to make an API request where multiple ids can be submitted. The django rest framework tutorial shows how to get all records from a model. For example http://127.0.0.1:8000/snippets/ will return all snippet records. The tutorial also shows how to retrieve a single item from a model. http://127.0.0.1:8000/snippets/2/ will return only snippet record with pk=2. I'd like to be able to request multiple records, but not all records. How could I change this

Creating a profile model with both an InlineAdmin and a post_save signal in Django

喜夏-厌秋 提交于 2019-12-30 04:11:19
问题 I created a 'profile' model (with a 1-to-1 relationship to the User model) as described on Extending the existing user model. The profile model has an optional many-to-one relationship to another model: class Profile(models.Model): user = models.OneToOneField(User, primary_key=True) account = models.ForeignKey(Account, blank=True, null=True, on_delete=models.SET_NULL) As documented there, I also created an inline admin: class ProfileInline(admin.StackedInline): model = Profile can_delete =

How to add through option to existing ManyToManyField with migrations and data in django

孤街醉人 提交于 2019-12-30 03:43:12
问题 I can't find reference to particular issue in docs or online. I have an existing many to many relation. class Books(models.Model): name = models.CharField(max_length=100) class Authors(models.Model): name = models.CharField(max_length=100) books = models.ManyToManyField(Books) This has migrations and data. Now I need to use through option in order to add one extra field in table holding many to many relation. class Authorship(models.Model): book = models.ForeignKey(Books) author = models

Django 1.7.1 Makemigrations fails when using lambda as default for attribute

纵饮孤独 提交于 2019-12-30 02:44:05
问题 I'm using Django 1.7.1. My model looks like this: from datetime import datetime from django.db import models class myModel(models.Model): x = models.CharField(max_length=254,null=True, blank=True,) Everything works perfectly fine. However, when I add the following attribute to myModel, it breaks: y = models.DateTimeField(default=lambda: datetime.utcnow() + timedelta(days=1), editable=False) manage.py makemigrations gives me the following error: ValueError: Cannot serialize function: lambda

Django Model set foreign key to a field of another Model

会有一股神秘感。 提交于 2019-12-30 01:41:06
问题 Is there any way to set a foreign key in django to a field of another model? For example, imagine I have a ValidationRule object. And I want the rule to define what field in another model is to be validated (as well as some other information, such as whether it can be null, a data-type, range, etc.) Is there a way to store this field-level mapping in django? 回答1: I haven't tried this, but it seems that since Django 1.0 you can do something like: class Foo(models.Model): foo = models

Is there a clever way to get the previous/next item using the Django ORM?

无人久伴 提交于 2019-12-30 00:55:11
问题 Say I have a list of photos ordered by creation date, as follows: class Photo(models.Model): title = models.Char() image = models.Image() created = models.DateTimeField(auto_now_add=True) class Meta: ordering = ('-created',) I have an arbitrary Photo object photo_x. Is there an easy way to find the previous and next photos by position in the queryset? Also, I would like to wrap around if I am at the beginning/end and have it not fail if the are only 1 or 2 photos. 回答1: You're in luck! Django

django model choice option as a multi select box

半世苍凉 提交于 2019-12-30 00:42:15
问题 Assuming that I have such model COLORS= ( ('R', 'Red'), ('B', 'Yellow'), ('G', 'White'), ) class Car(models.Model): name = models.CharField(max_length=20) color= models.CharField(max_length=1, choices=COLORS) It displays as a selectbox in the admin panel however I would like my admin-user to multi select those colors like many-to-many relationship, how can this be achieved without a ('RB', 'Red&Blue'), type of logic 回答1: Can a Car have multiple color s? In that case color ought to be a many

Django model group by datetime's date

♀尐吖头ヾ 提交于 2019-12-30 00:17:09
问题 Assume I have a such model: class Entity(models.Model): start_time = models.DateTimeField() I want to regroup them as list of lists which each list of lists contains Entities from the same date (same day, time should be ignored). How can this be achieved in a pythonic way ? Thanks 回答1: Create a small function to extract just the date: def extract_date(entity): 'extracts the starting date from an entity' return entity.start_time.date() Then you can use it with itertools.groupby: from itertools

Edit/show Primary Key in Django Admin

感情迁移 提交于 2019-12-30 00:14:29
问题 It appears Django hides fields that are flagged Primary Key from being displayed/edited in the Django admin interface. Let's say I'd like to input data in which I may or may not want to specify a primary key. How would I go about displaying primary keys in the admin interface, and how could I make specifying it optional? 回答1: I also wanted to simply show the 'id' (primary key) within the Django admin, but not necessarily edit it. I just added it to the readonly_fields list, and it showed up