Django - last insert id

2024/10/13 19:31:09

I can't get the last insert id like I usually do and I'm not sure why.

In my view:

comment = Comments( ...)
comment.save()
comment.id #returns None

In my Model:

class Comments(models.Model):id = models.IntegerField(primary_key=True)

Has anyone run into this problem before? Usually after I call the save() method, I have access to the id via comment.id, but this time it's not working.

Answer

Are you setting the value of the id field in the comment = Comments( ...) line? If not, why are you defining the field instead of just letting Django take care of the primary key with an AutoField?

If you specify in IntegerField as a primary key as you're doing in the example Django won't automatically assign it a value.

https://en.xdnf.cn/q/69490.html

Related Q&A

How to check if default value for python function argument is set using inspect?

Im trying to identify the parameters of a function for which default values are not set. Im using inspect.signature(func).parameters.value() function which gives a list of function parameters. Since Im…

OpenCV-Python cv2.CV_CAP_PROP_POS_FRAMES error

Currently, I am using opencv 3.1.0, and I encountered the following error when executing the following code:post_frame = cap.get(cv2.CV_CAP_PROP_POS_FRAMES)I got the following error Message:File "…

How to get the total number of tests passed, failed and skipped from pytest

How can I get to the statistics information of a test session in pytest?Ive tried to define pytest_sessionfinish in the conftest.py file, but I only see testsfailed and testscollected attributes on th…

Tensorflow ArgumentError Running CIFAR-10 example

I am trying to run the CIFAR-10 example of Tensorflow. However when executing python cifar10.py I am getting the error attached below. I have installed Version 0.6.0 of the Tensorflow package using pip…

How to plot multiple density plots on the same figure in python

I know this is going to end up being a really messy plot, but I am curious to know what the most efficient way to do this is. I have some data that looks like this in a csv file:ROI Band Min…

Running Tkinter on Mac

I am an absolute newbie. Im trying to make Python GUI for my school project so I decided to use Tkinter. When I try to import Tkinter it throws this message: >>> import tkinter Traceback (most…

Object-based default value in SQLAlchemy declarative

With SQLAlchemy, it is possible to add a default value to every function. As I understand it, this may also be a callable (either without any arguments or with an optional ExecutionContext argument).No…

High Resolution Image of a Graph using NetworkX and Matplotlib

I have a python code to generate a random graph of 300 nodes and 200 edges and display itimport networkx as nx import matplotlib.pyplot as pltG = nx.gnm_random_graph(300,200) graph_pos = nx.spring_layo…

how to read an outputted fortran binary NxNxN matrix into Python

I wrote out a matrix in Fortran as follows:real(kind=kind(0.0d0)), dimension(256,256,256) :: dense[...CALCULATION...]inquire(iolength=reclen)dense open(unit=8,file=fname,& form=unformatted,access=d…

python argparse subcommand with dependency and conflict

I want to use argparse to build a tool with subcommand. The possible syntax could be/tool.py download --from 1234 --interval 60 /tool.py download --build 1432 /tool.py clean --numbers 10So I want to us…