modelform

Generating unique slug in Django

北慕城南 提交于 2019-12-14 01:11:54
问题 I have seen a variety of different methods for generating unique slugs: Ex. 1, Ex.2, Ex. 3, Ex. 4, etc. etc. I want to create unique slugs upon saving a ModelForm. If my models are like: class Phone(models.Model): user = models.ForeignKey(User) slug = models.SlugField(max_length=70, unique=True) year = models.IntegerField() model = models.ForeignKey('Model') series = models.ForeignKey('Series') Say that the Phone object has the following values (via submitted ModelForm): Phone.user = dude

Django save only first form of formset

不打扰是莪最后的温柔 提交于 2019-12-13 06:29:11
问题 I've looked through every similar question (and tried them), but still couldn't find answer. I have two models: class Project(models.Model): author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) name = models.CharField(max_length=120, verbose_name = "Название проекта") url = models.URLField(max_length=120, unique=True, verbose_name = "Полный адрес сайта") robots_length = models.CharField(max_length=5, default=0) updated = models.DateTimeField(auto_now=True, auto_now_add=False)

How to upload multiple images using modelformset - Django

こ雲淡風輕ζ 提交于 2019-12-13 05:52:57
问题 I am trying to create a system which allows a user to be able to add a new Vegetable object, upload a thumbnail and multiple images and files - all from the AddVegetable page, and be able to output this easily as the vegetable types are filtered to display on different pages I'm trying to achieve this with the code below but it won't work and I cant figure out why exactly, as it stands I'm getting a KeyError 'image', but I don't know why. I am going about this the right way at all? I am

Initialize form with request.user in a ModelForm django

我是研究僧i 提交于 2019-12-13 03:03:53
问题 I have this ModelForm class ScheduleForm(forms.ModelForm): class Meta: model = Schedule fields = ['name', 'involved_people',] def __init__(self, user, *args, **kwargs): super(ScheduleForm, self).__init__(*args, **kwargs) self.fields['involved_people'].queryset = Profile.objects.exclude(user=user) This is my view def create_schedule(request): form = ScheduleForm(request.POST or None) schedules = Schedule.objects.all().order_by('deadline_date') if form.is_valid(): schedule = form.save(commit

how to receive modelform_instance.cleaned_data['ManyToMany field'] in view when form field is ModelMultipleChoiceField?

天涯浪子 提交于 2019-12-13 02:42:54
问题 Here is the situation: I have a model as below: class School(Model): name = CharField(...) Permit model has three objects: School.objects.create(name='school1') # id=1 School.objects.create(name='school2') # id=2 I have another model: Interest(Model): school_interest = ManyToManyField(School, blank=True,) I then build a ModelForm using Interest: class InterestForm(ModelForm): school_interest = ModelMultipleChoiceField(queryset=School.objects.all(), widget=CheckboxSelectMultiple, required

Django for loop overwrites saved modelform

…衆ロ難τιáo~ 提交于 2019-12-13 01:55:35
问题 The following code saves to database but the values inputted to the form2 overwrites values inputted to Form1 in database. However the Assumptions.Name is not overwritten and has both values Form1 and Form2. Also, if I refresh the page form is expanding and have more rows with previously saved values in it. How to avoid and save Form1 and Form2 data to database correctly? views.py from django.shortcuts import render from .forms import modelformset_factory, AssumptionsForm from .models import

django - queryset in modelForm

北战南征 提交于 2019-12-12 14:21:12
问题 I need to filter Food model by datetime in forms.py , but I do not know how to do it. Could anyone help me? models.py class Food(models.Model): class Meta: verbose_name = "Food" verbose_name_plural = "Foods" def __unicode__(self): return self.food_name food_name = models.CharField(verbose_name="Food Name", max_length=50) serve_date = models.DateTimeField(verbose_name="Serve Date") forms.py class Reserve(forms.ModelForm): food_name = forms.ModelChoiceField( queryset=Food.objects.all(), widget

Trying to save my Django Model Formset, keep getting ManagementForm error?

不羁岁月 提交于 2019-12-12 04:07:47
问题 So, a total Django Model Formset Newb question. I'm trying to save my form and keep getting this error: ['ManagementForm data is missing or has been tampered with'] Here is what I have for my TemplateView: class AttendanceTemplate(TemplateView): template_name = 'attendance/index.html' def get_context_data(self, **kwargs): context = super(AttendanceTemplate, self).get_context_data(**kwargs) instruction = Instruction(self.request.user.username) sections_list = self.request.GET.getlist('sections

How to remove a field from modelform model instance?

人走茶凉 提交于 2019-12-11 15:22:54
问题 I have a question related this one: How to handle the validation of the model form when the model has a clean method if the model form excluded some fields? This is my model: class StudentIelts(Model): SCORE_CHOICES = [(float(i/2), float(i/2)) for i in range(0, 19)] IELTS_TYPE_CHOICES = [('General', 'General'), ('Academic', 'Academic'), ] student = OneToOneField(Student, on_delete=CASCADE) has_ielts = BooleanField(default=False, ) ielts_listening = FloatField(choices=SCORE_CHOICES, null=True,

Django using a newly create object in reverse redirect

怎甘沉沦 提交于 2019-12-11 03:05:14
问题 I am trying to pull the id from the newly created project object so I can redirect the user to the page containing the new project. Right now I get "'ProjectAddForm' object has no attribute 'id'". I have read online that this should work but for some reason it's not. if request.method == 'POST': form = ProjectAddForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('project.views.detail', args=(form.id))) Forms.py class ProjectAddForm(forms.ModelForm): class