Save user first_name as default value for model django

2024/10/9 0:45:11

I have an article model with author variable which I want to save as the users first and last name. I use custom user model called Account.

author = models.CharField('author',max_length=50 default=User.first_name)

When saved it shows the author is <django.db.models.query_utils.DeferredAttribute object at 0x10b461dc0> So how can I retrieve the account first name when saving this form:

form = ArticleCreationForm(request.POST) # check if form data is valid if request.method == "POST":if form.is_valid(): form.save() # save the form data to model context['form']= form 

forms.py:

class ArticleCreationForm(forms.ModelForm): # specify the name of model to use class Meta: model = Article fields = "__all__"exclude = ['votes', 'author']

It would be good if you can make it so that the user sees the author field with his name but can't edit it.

Answer

you can use the below mentioned codes for generating the author model and use it corresponding to the post they create :

from django.contrib.auth import get_user_model
User = get_user_model()
class Author(models.Model):user = models.OneToOneField(User, on_delete=models.CASCADE)profile_picture = models.ImageField()def __str__(self):return self.user.username

and you can call this user/author where u need in your model using :

author = models.ForeignKey(Author, on_delete=models.CASCADE)
https://en.xdnf.cn/q/118654.html

Related Q&A

How to compare two imagefile from two different files in python

I would like to create a program that compares two images. I need to take images from two different folders and compare that images if they are same or not. Then I want to print out as same or differen…

Cant import from module despite presence of __init__.py

I have the following folder structureproject_folder/pyutils/__init__.pyscript1.pyscript2.py lambdas/__init__.pylambda_script1.pylambda_script2.pylambda_tests/__init__.pylambda_test1.pyWithin lambda_tes…

Multiple strings in one variable

Say, for example, I have some code that goes like this: sentence = input("Enter input: ") target_letter = "a" or "b" or "c" print(sentence.index(target_letter))M…

How to get the area of shape filled using turtle

I graphed a fractal shape in Python using turtle, and am trying to get the area of this fractal after a sufficiently high iteration. This fractal is related to the Koch snowflake, for those interested.…

What distinguishes a command from needing () vs not?

I recently spent way too long debugging a piece of code, only to realize that the issue was I did not include a () after a command. What is the logic behind which commands require a () and which do not…

python iterate yaml and filter result

I have this yaml file data:- name: acme_aws1source: awspath: acme/acme_aws1.zip- name: acme_gke1source: gkepath: acme/acme_gke1.zip- name: acme_ocisource: ocipath: acme/acme_oci1.zip- name: acme_aws2so…

Faster way to looping pixel by pixel to calculate entropy in an image

I have been calculating the entropy of an image with a pixel by pixel convolution operation, and it has been working but very slowly, increasing the execution time with the kernel size. Here is my func…

Google AppEngine - updating my webapp after deploy

friends! Im fairly new to web app world and I have a question regarding Google AppEngine functions. Ive installed the Launcher on my machine and signed up for the online platform (Python). Ive added …

Extract GPS coordinates from .docx file with python

I have some hectic task to do for which I need some help from python. Please see this word document.I am to extract texts and GPS coordinates from each row. There are currently over 100 coordinates in …

How to Serialize SQL data after a Join using Marshmallow? (flask extension)

I have 2 tables in SQL: class Zoo(db.Model):id = db.Column(db.Integer, primary_key=True)nome = db.Column(db.String(80), unique=True, nullable=False)idade = db.Column(db.Integer, unique=False, nullable=…