For some reason, the exclude in my forms isn't working and the primary key of my Item models is showing up on my formset. How can I get rid of it?
Form:
class ItemForm(forms.ModelForm):
class Meta:
model = Item
fields = ('name',
'description',
'quantity',
'start',
'end',
'cost_price',
'selling_price',)
widgets = {
'cost_price': forms.TextInput(attrs={'onChange':'updateSellingPrice()'}),
'description': forms.Textarea,
'start': SelectDateWidget,
'end': SelectDateWidget}
exclude = ('id')
ItemFormSet = modelformset_factory(Item, form=ItemForm, max_num=5, extra=3, exclude=('id'))
View:
def item_details(request, event_slug, role_id, module_slug):
event = get_object_or_404(Event, slug=event_slug)
payment_details = EventPaymentDetail.objects.get_or_create(event=event)[0]
try:
item_details = Item.objects.filter(event=event)
except:
item_details = Item.objects.get_or_create(event=event)[0]
if request.method == 'POST':
item_formset = ItemFormSet(request.POST)
#display_error(request, item_formset)
if item_formset.is_valid():
instances = item_formset.save(commit=False)
for instance in instances:
instance.event = event
instance.save()
messages.success(request, 'Item details successfully saved!')
url = reverse('event_admin_dashboard', args=[event_slug, role_id])
return redirect(url)
else:
item_formset = ItemFormSet()
currency_type = payment_details.currency
template = 'registration/item_details.html'
return render(request, template, locals())
I don't believe it is possible to exclude the id field. I'm afraid I can't give you an explanation or a link to the docs.
Aside:
It's not the issue here, but you're missing a comma on your exclude tuple. That's not the problem here, but it means that django interprets it as
exclude = ('i', 'd')
It should be:
exclude = ('id',)
Related
I'm creating an update view using django-form for updating one of my objects that have the following fields:
class Object(models.Model):
name = models.CharField(max_length=40)
logo = models.ImageField(upload_to='object_logo/')
text_1 = models.TextField()
text_2 = models.TextField()
So, i have created the following form in forms.py:
class ObjectForm(forms.ModelForm):
class Meta:
model = Object
fields = [
'name',
'logo',
'text_1',
'text_2',
]
labels = {
'name': 'Name',
'logo': 'Logo',
'text_1': 'Text 1',
'text_2': 'Text 2',
}
and defined the following view called update_object:
def update_object(request, value):
object = get_object_or_404(Object, pk=value)
if request.method == "POST":
form = ObjectForm(request.POST, request.FILES)
if form.is_valid():
object.name = form.cleaned_data['name']
object.logo = form.cleaned_data['logo']
object.text_1 = form.cleaned_data['text_1']
object.text_2 = form.cleaned_data['text_2']
object.save()
return HttpResponseRedirect(reverse('myApp:detail_object', args=(value, )))
else:
form = ObjectForm(
initial={
'name': object.name,
'logo': object.logo,
'text_1': object.text_1,
'text_2': object.text_2,
}
)
context = {'object': object, 'form': form}
return render(request, 'myApp/update_object.html', context)
My problem is: even with an "initial" value stetted up for logo, i have to select an image every time i will update my object (otherwise i receive the update_object page with the message "This field is required").
Is there a way to make the current object.logo as the pre-defined value of the input in my ObjectForm in the update_object view?
I've already tried to set blank = True in the logo model field (which was a bad idea). I also thought in make an alternative conditional code for form.is_valid() but i dont know how to do it.
Update your forms.py like so:
class ObjectForm(forms.ModelForm):
class Meta:
model = Object
fields = '__all__'
...and views.py:
def update_object(request, value):
object = get_object_or_404(Object, pk=value)
if request.method == "POST":
form = ObjectForm(request.POST, request.FILES, instance=object)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('myApp:detail_object', args=(value, )))
else:
form = ObjectForm(instance=object)
context = {'object': object, 'form': form}
return render(request, 'myApp/update_object.html', context)
It can be done like this (more clean)
In case share your template code
serializers.py
class JobSerializer(serializers.ModelSerializer):
# image = Base64ImageField(max_length=None,
# use_url=True)
# applicant = serializers.ForeignKe
applicant = serializers.StringRelatedField(many=True)
email = serializers.SerializerMethodField("get_username_from_user")
company_name = serializers.SerializerMethodField("get_company_name_from_user")
class Meta:
model = Jobs
fields = ['company_name', 'email', 'title', 'desc', 'image', 'price', 'category', 'applicant']
# extra_kwargs = {"email": {"required": False}}
def get_username_from_user(self, jobs):
email = jobs.user.email
return email
def get_company_name_from_user(self, jobs):
company_name = jobs.user.company_name
return company_name
views.py
#api_view(['GET'])
#permission_classes([IsAuthenticated])
def api_detail_jobs_view(request, id):
try:
jobs = Jobs.objects.get(id=id)
except Jobs.DoesNotExist:
data = {}
data['response'] = "Job does not exist"
return Response(data, status=status.HTTP_404_NOT_FOUND)
if request.method == "GET":
serializer = JobSerializer(jobs)
user = request.user
if user == serializer.email:
data = {}
auth_show = serializer
data['title'] = auth_show.title
data['applicant'] = auth_show.applicant
return Response(data)
else:
no_auth_show = serializer
data = {}
data['title'] = no_auth_show.title
return Response(data)
here is serializers.py in which 'email' is included. i know i am missing something very clear but it took hours to realise :) so any help will be appriciated
i am trying to show 'applicants' only to users who owns the 'job' but i can't pass 'email' from serializer in to the view. I can't pass any attribute from serializer in to data dict.
As shown in the documentation of DRF, after serializing an object you can access its fields via the data attribute. So it would look like serializer.data['email'].
And one extra tip, consider using the Django shortcut get_object_or_404() instead of that try/except block. Good luck!
I've got 2 questions:
1) I'm displaying a list of users w/ their group,and for the group name I got: ]> using {{instance.groups.all}} in my template. Any ideas to have only mygroup displayed?
2) For my update user form, I got an error and I don't know how and where to resolve it.
error: updateUserView() got an unexpected keyword argument 'id'
forms.py
class UpdateForm(UserChangeForm):
is_active = forms.BooleanField()
Group = [('Viewers', 'Viewers'), ('Editors', 'Editors'), ('Creators', 'Creators'), ]
group_name = forms.ChoiceField(choices=Group)
class Meta:
model = User
fields = ('email', 'is_active', 'group_name', )
views.py
def updateUserView(request):
if request.method == 'POST':
form = UpdateForm(request.POST, instance=request.user)
if form.is_valid():
user = form.save()
group = Group.objects.get(name=request.POST.get('group_name'))
user.groups.add(group)
return redirect('accounts:users')
else:
form = UpdateForm(instance=request.user)
return render(request, 'accounts/update_user.html', {'form': form})
class UserView(LoginRequiredMixin, GroupRequiredMixin, ListView):
template_name = 'accounts/display_users.html'
group_required = ['Creators']
queryset = User.objects.filter(is_active=True)
instead of using
group = Group.objects.get(name=request.POST.get('group_name'))
try using
group = form.cleaned_data.get('Group')
To solve the error error: updateUserView() got an unexpected keyword argument 'id'
you need to pass the id to the view. Because your url is probably like this '/user/<int:id>/'
def updateUserView(request, id):
if request.method == 'POST':
........
Here is the error: Any help in undestanding what is happening and how to fix it would be much appreciated
Cannot assign '<PartnerForm bound=True, valid=True, fields=(name;logo;banner_image;mission;vision;website_link;fb_link;twitter_link;ig_link)>': "Product.partner" must be a "Partner" instance.
I am trying to add a one to many relationship between two models. The 'one' model is a 'Partner. The 'many' are 'Product' which contain image description, price, etc.
Here are my views.py
def partner_create(request):
#Trying to add multiple product functionality
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
ProductFormSet = inlineformset_factory(Partner, Product, form=ProductForm, extra=3)
if request.method == 'POST':
partnerForm = PartnerForm(request.POST or None, request.FILES or None)
formset = ProductFormSet(request.POST, request.FILES, queryset=Product.objects.none())
if partnerForm.is_valid() and formset.is_valid():
instance = partnerForm.save(commit=False)
instance.save()
for form in formset.cleaned_data:
image = form['image']
product = Product(partner=partnerForm, image=image)
product.save()
messages.success(request, "Partner Successfully Created")
else:
print partnerForm.errors, formset.errors
else:
partnerForm = PartnerForm()
formset = ProductFormSet(queryset=Product.objects.none())
return render(request, "partner_form.html", {"partnerForm": partnerForm, "formset": formset})
here is my admin.py
class ProductImageInline(admin.TabularInline):
model = Product
extra = 3
#This works originally but doesn't do multiple products
class PartnerModelAdmin(admin.ModelAdmin):
list_display = ["__unicode__", "timestamp"]
inlines = [ProductImageInline,]
class Meta:
model = Partner
admin.site.register(Partner, PartnerModelAdmin)
Here is my form.py
class PartnerForm(forms.ModelForm):
mission = forms.CharField(widget=PagedownWidget(show_preview=False))
vision = forms.CharField(widget=PagedownWidget(show_preview=False))
# publish = forms.DateField(widget=forms.SelectDateWidget)
class Meta:
model = Partner
fields = [
"name",
"logo",
"banner_image",
"mission",
"vision",
"website_link",
"fb_link",
"twitter_link",
"ig_link",
]
class ProductForm(forms.ModelForm):
image = forms.ImageField(label='Image')
class Meta:
model = Product
fields = [
"partner",
"image",
"description",
"price"
]
The error is pretty obvious. In the lines
product = Product(partner=partnerForm, image=image)
product.save()
you are assigning a PartnerForm to your product while it expects a Partner instance! Change it to
product = Product(partner=instance, image=image)
product.save()
(instance is a Partner instance from what I see above) and it will work.
Hi (sorry for my bad english), i'm trying to show a form using a number input instead a select in a FK Relation on django model, but i cant make it.
The thing is that i need to fill the ID of the product manually writing the id in a input box, but django automatically makes the select form with all the products!
this is what i have in forms.py
class PurchaseForm(forms.ModelForm):
class Meta:
model = Purchase
fields = [
'date',
'supplier',
'warehouse',
]
widgets = {
'date': forms.DateInput(attrs={
'type': 'date',
}),
}
class PurchaseItemForm(forms.ModelForm):
class Meta:
model = PurchaseItem
fields = [
'product',
'quantity',
'net_purchase_price',
'sell_price',
]
widgets = {
'product': forms.NumberInput(),
}
PurchaseFormSet = inlineformset_factory(
Purchase,
PurchaseItem,
fields='__all__',
extra = 10,
)
this is my view
class PruchaseCreateView(CreateView):
template_name = 'purchases/create_purchase.html'
form_class = PurchaseForm
def get_context_data(self, **kwargs):
context = super(PruchaseCreateView, self).get_context_data(**kwargs)
if self.request.POST:
context['formset'] = PurchaseFormSet(self.request.POST)
else:
context['formset'] = PurchaseFormSet()
return context
def form_valid(self, form):
context = self.get_context_data()
formset = context['formset']
if formset.is_valid():
self.object = form.save()
formset.instance = self.object
formset.save()
return redirect(self.object.get_absolute_url())
else:
return self.render_to_response(self.get_context_data(form=form))
i try with the widget NumberInput but nothing!!! any idea?
You need to include the custom form when you call inlineformset_factory:
PurchaseFormSet = inlineformset_factory(
Purchase,
PurchaseItem,
form=PurchaseItemForm
fields='__all__',
extra = 10,
)