django-models

Are django signals also included inside of the transaction.atomic decorator?

不想你离开。 提交于 2020-01-01 08:46:08
问题 I have a model file that uses a post_save signal to create a linked row in another table. In typical fashion, I can create a page from one of my views which is decorated with @transaction.atomic. I would like to know if this decorator will put the creation of the Page object and the SharedPage object in the same transaction. It is not clear from the django docs that signals are a part of this atomic transaction. models.py class Page(models.Model): name = models.CharField(default='My default

Limit Maximum Choices of ManyToManyField

别说谁变了你拦得住时间么 提交于 2020-01-01 08:45:22
问题 I'm trying to limit the maximum amount of choices a model record can have in a ManyToManyField. In this example there is a BlogSite that can be related to Regions. In this example I want to limit the BlogSite to only be able to have 3 regions. This seems like something that would have been asked/answered before, but after a couple hours of poking around I haven't been able to find anything close. For this project, I'm using Django 1.3. #models.py class BlogSite(models.Model): blog_owner =

Django: Filtering datetime field by *only* the year value?

℡╲_俬逩灬. 提交于 2020-01-01 08:32:49
问题 I'm trying to spit out a django page which lists all entries by the year they were created. So, for example: 2010: Note 4 Note 5 Note 6 2009: Note 1 Note 2 Note 3 It's proving more difficult than I would have expected. The model from which the data comes is below: class Note(models.Model): business = models.ForeignKey(Business) note = models.TextField() created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) class Meta: db_table = 'client_note'

Getting a “The following content types are stale and need to be deleted” when trying to do a migrate. What does this mean, and how can I solve it?

懵懂的女人 提交于 2020-01-01 08:25:11
问题 This is my models.py: class Notification(models.Model): user = models.ForeignKey(User) createdAt = models.DateTimeField(auto_now_add=True, blank=True) read = models.BooleanField(default=False, blank=True) class Meta: abstract = True class RegularNotification(Notification): message = models.CharField(max_length=150) link = models.CharField(max_length=100) class FNotification(Notification): # same as Notification pass When I do python manage.py makemigrations , this is what it says: Migrations

TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

落花浮王杯 提交于 2020-01-01 08:20:36
问题 my models.py: class Attendancename(models.Model): teacher_name = models.ForeignKey(Teachername, default='Ram') date = models.DateField('Date', default=datetime.datetime.today) intime = models.TimeField('IN-TIME', auto_now=True) outtime = models.TimeField('OUT-TIME', auto_now=True) def hours_conversion(self): startdelta = datetime.timedelta(hours=self.intime.hours, minutes=self.intime.minutes, seconds=self.intime.seconds) enddelta = datetime.timedelta(hours=self.outtime.hours, minutes=self

How to hide a field in django modelform?

跟風遠走 提交于 2020-01-01 06:57:10
问题 For example: class TestModel(models.Model): ref1 = models.ForeignKey(RefModel) text1 = models.TextField() class TestModelForm(ModelForm): class Meta: model = TestModel fields = ('text1') I only allow the user to input text1 field, but when I redefine the post method of my view, I also want to set the ref1 value, how should I do that? I wish I can let TestModelForm has the ref1 field but don't let user modify it, then I can modify the value of request.POSt in post method, and pass it to

Can I create django model, which will be not persisted in database?

百般思念 提交于 2020-01-01 06:51:10
问题 As asked in question: Can I create django model(models.Model), which will be not persisted in database? The reason is I want use it in my django admin to build custom form basing on forms.ModelForm 回答1: You can override the model's save() method to prevent saving to the database, and use managed = False to prevent Django from creating the appropriate tables: class NonPersistantModel(models.Model): def save(self, *args, **kwargs): pass class Meta: managed = False Note that this will raise an

Django REST Serializer doing N+1 database calls for multiple nested relationship, 3 levels

廉价感情. 提交于 2020-01-01 06:11:22
问题 I have a situation where my model has a Foreign Key relationship: # models.py class Child(models.Model): parent = models.ForeignKey(Parent,) class Parent(models.Model): pass and my serializer: class ParentSerializer(serializer.ModelSerializer): child = serializers.SerializerMethodField('get_children_ordered') def get_children_ordered(self, parent): queryset = Child.objects.filter(parent=parent).select_related('parent') serialized_data = ChildSerializer(queryset, many=True, read_only=True,

Django and ChartJS

大憨熊 提交于 2020-01-01 05:46:08
问题 I'm trying to understand if it it's possible to incorporate dynamic data into a Django Chart JS architecture. I went through a couple of tutorials and ultimately got Django to work with ChartJS and it's very good when I'm able to hard code values and then display the related graphs. What I'm ultimately trying to do is this same exercise with dynamic data from my database. I found this identical question in SO, https://stackoverflow.com/questions/47575896/dynamic-chart-using-django-and-chart

How can I override get method in django Model?

限于喜欢 提交于 2020-01-01 05:34:27
问题 I want to do encode the data before saving it to a database table and decode it after reading it from the database table. I wanted to override django get and save methods. something like: class UserData(models.Model): userid = models.IntegerFields data = models.charField(max_length=25) def save(self, *args, **kwargs): encode_data(self.data) super(UserData, self).save(*args, **kwargs) def get(self, *args, **kwargs): data = super(UserData, self).get(*args, **kwargs) return decode_data(data)