Comments (3)
Can you please post some code so itβs easier to understand.
from django-addanother.
In my case I have a model "project" that contains a CharField "title", and a ForeignKey "company". For "company" I want to use the AddAnotherWidget so the user can add companies (there's more to a "project" of course, but I try to limit the code to only what is relevant):
models:
from django.db import models
class Project(models.Model):
title = models.CharField(max_length = 255)
company = models.ForeignKey(Company, on_delete = models.DO_NOTHING, blank = True, null = True)
def __str__(self):
if self.start_date is None:
return f"{self.company.__str__()}: {self.title.__str__()} "
else:
return f"{self.company.__str__()}: {self.title.__str__()} ({str(self.start_date.year)})"
class Meta:
ordering = [Lower('company'), Lower('title')]
class Company(models.Model):
name = models.CharField(max_length = 255, unique = True, blank = False)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Companies"
ordering = [Lower('name')]
views (project_view is function based as this is what I knew when I started while view for company is class based as this is a requirement for AddAnother if I understood well):
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib import messages
from django_addanother.views import CreatePopupMixin
from ..forms import *
from ..models import *
def project_view(request, pk):
if request.user.is_authenticated:
project = Project.objects.get(pk = pk)
request.user.last_watched_project = pk
request.user.save()
if request.method == 'GET':
form = ProjectForm(initial = project.__dict__)
context = {
'form': form,
'jquery': 'admin/js/vendor/jquery/jquery.js',
}
return render(request, "main/project.html", context)
if request.method == 'POST':
form = ProjectForm(request.POST)
context = {
'form': form,
'jquery': 'admin/js/vendor/jquery/jquery.js',
}
if not request.user.is_superuser:
messages.error(request, "You do not have the proper rights to change this.")
return render(request, "main/project.html", context)
if form.is_valid():
project = form.save()
messages.success(request, "Project saved.")
return render(request, "main/projects.html")
else:
messages.error(request, "One or more required fields are empty.")
return render(request, "main/project.html", context)
class CreateCompany(CreatePopupMixin, generic.CreateView):
model = Company
fields = ['name']
forms:
from django import forms
from django_addanother.widgets import AddAnotherWidgetWrapper
from main.models import *
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ('title', 'company')
widgets = {
'company': AddAnotherWidgetWrapper(
forms.Select(),
'/company',
),
}
class CompanyForm(forms.ModelForm):
class Meta:
model = Project
fields = '__all__'
urls:
from django.conf.urls.static import static
from django.urls import path
from references import settings
from . import views
app_name = 'main'
urlpatterns = [
path("", views.home, name = "home"),
path("project/<int:pk>/", views.project_view, name = "project_view"),
path("company", views.company_view.CreateCompany.as_view(), name = "company_view"),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
Problem: Let's say I made a project with name = "first project" and company = "EvilCorp". If I check in admin this is saved properly, even if I created "EvilCorp" on the spot using the AddAnotherWidget (so that is working great!). When I load this project back to a form for editing however the field name correctly contains "first project", while company gives the default value of "------". Same goes for other "classic" widgets versus AddAnotherWidgets.
Thanks for your support!
from django-addanother.
My bad, this wasn't an issue with AddAnotherWidgets, but my very own mistake.
in the views
form = ProjectForm(initial = project.__dict__)
should be
form = ProjectForm(instance = project)
from django-addanother.
Related Issues (20)
- Use bootstrap modal instead of pop-up? HOT 4
- Update fields of the same form HOT 4
- ModelSelect2AddAnother HOT 9
- Edit-button doesn't get the fk using DAL HOT 4
- How to add multiple stack popups? HOT 3
- xvfb broken HOT 9
- Release 2.0.1 on pypi HOT 4
- Error when using CreatePopupMixin on Django 3.0 HOT 3
- Flaky tests
- Change blocktrans in link title
- Widget state shared between form instances HOT 1
- Formset compatibility HOT 2
- Filter queryset with django-addanother
- Using addanother not for add but for choose ? HOT 2
- Django 4.1 support HOT 4
- Catch up with Django 4.1 features HOT 5
- 'AddAnotherWidgetWrapper' object has no attribute 'choices' HOT 2
- bug? or im doing something wrong? help appreciated HOT 8
- No popup when different variable name for form
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-addanother.