WebSep 11, 2024 · Edit for HTML solution. The value= on the is what gets passed to the POST data, so to ensure that the save view is getting a value it can save just change your option tag a little bit. Switch each.name to each.pk to give your view the value it needs. { { each.name }} . Share. Improve this answer. WebApr 30, 2012 · In the form definition, you set the ForeignKey to have the HiddenInput widget, and then use the get_form method on the view to set the value of your user: forms.py: from django import forms class ArticleForm (forms.ModelForm): class Meta: model = Article widgets = {"user": forms.HiddenInput ()} views.py:
#23449 (Saving models with unsaved ForeignKey) – Django
WebThe ForeignKey of your model should be the primary key of the table you are referencing: event = models.ForeignKey ('event') You would then reference the columns as such: foo.event.actor foo.event.receiver. If you wish you could also change the way your class/model references the foreign attributes with properties. WebFeb 20, 2024 · Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams loomis basin dolphins
django.db.utils.IntegrityError。FOREIGN KEY约束失败 - IT宝库
WebMar 30, 2014 · @Pieter, you have to do use commit = False, then you can attach user to your model then you .save () to to send to database. Don't try to form.save (commit=True) if you have relationship inside. – Valentin Garreau Jun 15, 2024 at 11:12 Add a comment 8 course has to be an instance of a Course model, not just the primary key of the instance. WebMar 21, 2024 · from django.db import models from django.contrib.auth.models import User class Blog (models.Model): user = models.ForeignKey (User) @property def username (self): return self.user.username With that property, you can access username through blog.username. Note on how to import User user = ForeignKey ('auth.User') or WebDjango Serializer Save models with foreign key relationships Ask Question Asked 7 years, 8 months ago Modified 7 years, 8 months ago Viewed 8k times 5 I am trying to save a model object that has a foreign key referencing another table. horaires bus 117