django-models

Cleaning data which is of type URLField

[亡魂溺海] 提交于 2020-01-14 04:03:52
问题 I have a simple URLField in my model link = models.URLField(verify_exists = False, max_length = 225) I would like to strip the leading and trailing spaces from the field. I don't think I can do this in "clean_fieldname" or in the "clean" method. Do I need to sub-class the "URLField" and remove the spaces in to_python method? Is there a better way to do this without any sub-classing? Edited This is my form class StoryForm(forms.ModelForm): title = forms.CharField(max_length=225, error_messages

Keeping track of how many views an object receives in Django

倖福魔咒の 提交于 2020-01-14 03:06:34
问题 For statistical purposes, I need to keep a log of each time a certain instance of a Model is viewed in Django. I started off by creating a separate model, Stats, that contains a ManyToMany field to another Model that stores the date and time of the access. Every time the object is accessed in a view, I update the associated Stats object. There are 2 problems with this approach (if not more): It violates the principle of not writing any data on a GET request. More importantly, it's really slow

What is the usage of `FilteredRelation()` objects in Django ORM (Django 2.X)?

荒凉一梦 提交于 2020-01-14 02:55:13
问题 I've seen Django 2.0 consists of FilteredRelation object in queryset. What is the usage of newly introduced FilteredRelation ? What I've looked into? I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation object. I looked into following code. But I didn't get it. >>> from django.db.models import FilteredRelation, Q >>> Restaurant.objects.annotate( ... pizzas_vegetarian=FilteredRelation( ... 'pizzas', condition=Q(pizzas__vegetarian=True), ... ), ... )

Django InlineModelAdmin gives error 'MediaDefiningClass' object is not iterable

情到浓时终转凉″ 提交于 2020-01-14 01:58:48
问题 From models.py class Indicator(models.Model): name = models.CharField(max_length=50) youtube = models.CharField(max_length=LONG_BLOG_POST_CHAR_LEN) description = models.CharField(max_length=LONG_BLOG_POST_CHAR_LEN) recommendation = models.CharField(max_length=LONG_BLOG_POST_CHAR_LEN) isPublic = models.BooleanField(default=False) methods_path = models.CharField(max_length=100,default=None) meta_description = models.CharField(max_length=150,default='') image_path = models.CharField(max_length

Why “class Meta” is necessary while creating a model form?

这一生的挚爱 提交于 2020-01-13 20:31:49
问题 from django import forms from .models import NewsSignUp class NewsSignUpForm(forms.ModelForm): class Meta: model = NewsSignUp fields = ['email', 'first_name'] here This code works perfectly fine. But, when I remove "class Meta:" as below, it throws a ValueError saying " ModelForm has no model class specified. " from django import forms from .models import NewsSignUp class NewsSignUpForm(forms.ModelForm): model = NewsSignUp fields = ['email', 'first_name'] Can someone please give an

The QuerySet value for an exact lookup must be limited to one result using slicing-Django

浪子不回头ぞ 提交于 2020-01-13 18:19:10
问题 I'm building a news website.While I tried to get the list of relative news which have the same tags.The error said: The QuerySet value for an exact lookup must be limited to one result using slicing-Django. I have two models News and Tag,Tag is a many to many foreign key of News. News model: class News(models.Model): tag = models.ManyToManyField(Tag, blank=True, verbose_name='tag') Tag model: class Tag(models.Model): name = models.CharField(max_length=40) View: def newsDetailView(request,

Django model form saving simultaneously post request twice

萝らか妹 提交于 2020-01-13 18:15:11
问题 I am using django-bootstrap-modal-forms 1.3.1 following https://pypi.org/project/django-bootstrap-modal-forms/ but if I run it's project of books it calls the post request to create book twice but saves the from once. As I am using it, it makes twice post request but both the request are saved one with empty file i.e one post saves all the fields of modal like title description , date but not the upload (file) and the next post saves all of it with upload simultaneously This is my model :

Django ModelChoiceField drop down box custom population

拜拜、爱过 提交于 2020-01-13 18:15:09
问题 I have a dropdown box that is being populated by a filtered list of objects from a model "Options". Currently, the dropdown list displays the names of each option. How would I get it to display another attribute from the same table? self.fields['name'] = forms.ModelChoiceField(queryset = Options.objects.filter(option_type = s), label = field_label, required=False) Quick example: drop down box currently displays the names of the cars: "Camero, Nissan, Honda" How would I get it to display the

Django, in many to many relationship within the self class, how do I reference each other in terms of ORM?

落花浮王杯 提交于 2020-01-13 18:04:14
问题 class User(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() gender = models.IntegerField() email = models.CharField(max_length=100) password = models.CharField(max_length=255) following = models.ManyToManyField("self", related_name='followers') objects = UserManager() def __repr__(self): return "User: {0}".format(self.name) In my model, User, users can follow and followed by each other. I can find who the user is following by this: user1 = User.objects.get(id

Django, in many to many relationship within the self class, how do I reference each other in terms of ORM?

北慕城南 提交于 2020-01-13 18:02:04
问题 class User(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() gender = models.IntegerField() email = models.CharField(max_length=100) password = models.CharField(max_length=255) following = models.ManyToManyField("self", related_name='followers') objects = UserManager() def __repr__(self): return "User: {0}".format(self.name) In my model, User, users can follow and followed by each other. I can find who the user is following by this: user1 = User.objects.get(id