Answer the question
In order to leave comments, you need to log in
How to make a binding to the parent model (object)?
Hello! There are 2 pages 'project_list' (displays a list of projects) and 'project_detail' (displays detailed information about a particular project). The 'project_detail' page has a link that should take the user to a page where they can add a new member to this particular project. How to intercept the id of the current project in the view in order to associate new members with the current project? At the moment, in the url, I define the project by its slug. I created my own form based on the model to add new members to the project and then applied this form to the view, but I'm not sure if I did it right?
models.py :
class Project(models.Model):
***FIELDS***
members = models.ManyToManyField(User, through='Membership',)
class Membership (models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE,)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
role = models.CharField(max_length=20, choices=ROLE_CHOICES,)
class ProjectMembersForm(forms.ModelForm):
class Meta:
model = Membership
exclude = ('project',)
url(r'^projects/(?P<project_id>\d+)/$', project_detail, name='project_detail'),
url(r'^projects/(?P<project_id>\d+)/members/add/$', add_project_member, name='add_project_member'),
def add_project_member(request, project_id):
project = get_object_or_404(Project, pk=project_id)
if request.method == 'POST':
form = ProjectMembersForm(request.POST)
if form.is_valid():
new_member = form.save(commit=False)
new_member.save()
return render(request, 'project/project_detail.html', {'new_member': new_member})
else:
form = ProjectMembersForm()
return render(request, 'project/project_members.html', {'form': form})
Traceback (most recent call last):
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Nurzhan\PycharmProjects\RMS\project\views.py", line 71, in add_project_member
new_member.save()
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\base.py", line 908, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\base.py", line 947, in _do_insert
using=using, raw=raw)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\query.py", line 1045, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\sql\compiler.py", line 1054, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sql_server\pyodbc\base.py", line 539, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: ('23000', '[23000] [Microsoft][ODBC SQL Server Driver][SQL Server]�� ������� �������� �������� NULL � ������� "project_id", ������� "RMS.dbo.project_membership"; � ������� ��������� �������� NULL. ������ � INSERT. (515) (SQLExecDirectW)')
[11/Mar/2017 21:35:38] "POST /ru/account/dashboard/projects/3/members/add/ HTTP/1.1" 500 143162
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question