Code Monkey home page Code Monkey logo

Comments (3)

jonashaag avatar jonashaag commented on June 26, 2024

Can you please post some code so it’s easier to understand.

from django-addanother.

Joris-Pieters avatar Joris-Pieters commented on June 26, 2024

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.

Joris-Pieters avatar Joris-Pieters commented on June 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.