I want to make a blog in Django.
The blog has Article, Author ... models.
each article has many resources. it looks like this on the article page:
Resources: Medium, Hashnode, Dev community, ...
I want that while adding an article, the admin can dynamically these article resources.
for the author, I want to add social links.
every author's social media are different. one may have Facebook, LinkedIn ... and the other may have Instagram, Twitter, ...
I want the admin (or author) can add any social media for the authors.
Update:
model implementation of Article:
class Article(models.Model):
"""Model definition for Article."""
title = models.CharField(
max_length=100
)
slug = models.SlugField(
max_length=100,
unique=True,
db_index=True
)
excerpt = models.TextField(
max_length=200
)
body = models.TextField(
max_length=10000,
validators=[MinLengthValidator(10)]
)
image = models.ImageField(
upload_to='images/blog',
)
image_resource = models.CharField(
max_length=100,
verbose_name="Image Resource Name",
null=True,
blank=True
)
related_articles = models.ManyToManyField(
'self',
symmetrical=True,
related_name='related_articles',
verbose_name='Related Articles',
blank=True
)
date_published = models.DateTimeField(
auto_now_add=True
)
author = models.ForeignKey(
Author,
on_delete=models.CASCADE,
related_name='Articles',
)
category = models.ForeignKey(
'Category',
on_delete=models.CASCADE,
related_name='Articles',
blank=True,
null=True
)
tags = models.ManyToManyField(
'Tag',
verbose_name='Tags'
)
model implementation of Author:
class Author(models.Model):
"""Model definition for Author."""
first_name = models.CharField(
max_length=50
)
last_name = models.CharField(
max_length=50
)
profile_pic = models.ImageField(
upload_to='profile_pics',
default='images/boy.jpg'
)
email = models.EmailField(
max_length=254
)
bio = models.TextField(
max_length=1000
)
facebook = models.URLField(
max_length=254,
blank=True,
null=True,
verbose_name='Facebook URL'
)
instagram = models.URLField(
max_length=254,
blank=True,
null=True,
verbose_name='Instagram URL'
)
twitter = models.URLField(
max_length=254,
blank=True,
null=True,
verbose_name='Twitter URL'
)
linkedin = models.URLField(
max_length=254,
blank=True,
null=True,
verbose_name='LinkedIn URL'
)
Look for an existing Django package that might handle what you want to have happen.
Aside from that, I can't think of a way that you can dynamically add these into a particular model. You'd have to include all of the different types of social media you feel an individual could possibly have ahead of of time.
You might be able to "fake it" by setting up a Profile for your authors that enables them to add a key:value pair into a field in the model as they desire to add social media accounts but that's about all I can suggest at the moment.
Related
When accessing my ExternalRecord model via the django admin screen, or by querying ExternalRecord.objects.all(), I receive the error: psycopg2.errors.UndefinedColumn: column integrations_externalrecord.oppcontact_id does not exist
I am building an integration functionality, and we have a junction table that houses an external id and the instance in our database that corresponds to this external id, set up like so:
class ExternalRecord(UUIDPrimaryKey, CreatedModifiedMixin, models.Model):
integration = models.ForeignKey(
to=Integration,
related_name='external_records',
on_delete=models.CASCADE
)
emailuser = models.ForeignKey(
"accounts.EmailUser",
related_name='external_records',
null=True,
blank=True,
on_delete=models.CASCADE
)
institution = models.ForeignKey(
"institutions.Institution",
related_name='external_records',
null=True,
blank=True,
on_delete=models.CASCADE
)
oppcontact = models.ForeignKey(
"opp_contacts.OppContact",
related_name='external_records',
null=True,
blank=True,
on_delete=models.CASCADE
)
external_record_id = models.CharField(
max_length=1000
)
and so on...
When I view the OppContact model either by viewing in the django admin screen or with OppContact.objects.all(), I see that the model has a field for "id". When I rollback to the migration before applying the oppcontact field, everything returns to work as normal, meaning I can query/view ExternalRecords without getting an error.
This is my OppContact model:
class OppContact(UUIDPrimaryKey):
company = models.ForeignKey(
"accounts.Company",
on_delete=models.CASCADE,
blank=True,
null=True
)
first_name = models.CharField(
max_length=100
)
last_name = models.CharField(
max_length=100
)...
And this is another model to which my ExternalRecord can be linked, Institution:
class Institution(UUIDPrimaryKey, CreatedModifiedMixin, models.Model):
company = models.ForeignKey(
"accounts.Company",
related_name='institutions',
null=False,
blank=False,
on_delete=models.CASCADE,
)
owner = models.ForeignKey(
"accounts.EmailUser",
related_name='institutions',
null=True,
blank=True,
on_delete=models.CASCADE,
)
name = models.CharField(max_length=100, null=False, blank=False)....
The only difference I see between the models is the OppContact doesn't have the CreatedModifiedMixin or models.Model, but I thought UUIDPrimaryKey extended models.Model, so I didn't think it mattered.
I have been stuck on this for several days, so any pointers in the right direction would be helpful (: Thank you all!
For all those following along at home, the issue was actually with a constraint I added to the ExternalRecord model, which caused the migration to fail, which is why the column wasn't found.
Is it possible to make a global filter in the admin panel?
For example, there are models:
class Company(models.Model):
name = models.CharField(
'Name company',
max_length=200,
help_text='Name company'
)
city = models.CharField(
'City',
max_length=200,
help_text='City'
)
class Object(models.Model):
name = models.CharField(
'Name object',
max_length=200,
help_text='Name object'
)
number = models.CharField(
'Number object',
max_length=200,
help_text='Number object'
)
company = models.ForeignKey(
Company,
on_delete=models.SET_NULL,
verbose_name='Company',
null=True
)
class Person(models.Model):
name = models.CharField(
'Name,
max_length=200,
help_text='Name'
)
number = models.CharField(
'ID person',
max_length=200,
help_text='ID person'
)
object = models.ForeignKey(
Object,
on_delete=models.SET_NULL,
verbose_name='Object',
null=True
)
It turns out that there are several companies. Each company has several facilities, which in turn have staff. I want to make a global filter by Companies in the admin panel. That is, not to filter every time, but let's say I chose the company "Horns and Hooves" and calmly worked in the admin area with objects belonging to this company.
This is just an example, in reality there are many more models
class Revision(models.Model):
date_created = models.DateTimeField(
db_index=True,
verbose_name=_("date created"),
help_text="The date and time this revision was created.",
)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=True,
null=True,
on_delete=models.SET_NULL,
verbose_name=_("user"),
help_text="The user who created this revision.",
)
comment = models.TextField(
blank=True,
verbose_name=_("comment"),
help_text="A text comment on this revision.",
)
I need to get the latest entry for each user. But I can’t build a normal query. I am using sqlite3 database
You can do something like this:
Revision.objects.values('user').annotate(Max('date_created'))
I am trying to create a custom Django Admin Site for new staff members that lets a staff member edit their User first_name/last_name + get presented a list of multiple choice questions with radio select for the answer choices. The questions need to be a predefined queryset (e.g. last 5 multiple choice questions by date_created), whether a M2M entries exists already or not.
models.py
class User(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid4,
unique=True,
)
first_name = models.CharField(
max_length=100,
blank=True,
null=True,
default=None,
)
last_name = models.CharField(
max_length=100,
blank=True,
null=True,
default=None,
)
class Questions(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid4,
unique=True,
)
question_text = models.CharField(
max_length=160,
blank=True,
null=True,
default=None,
)
date_created = models.DateTimeField(
auto_now_add=True
)
users = models.ManyToManyField(
Users,
through=Questions2Users,
through_fields=('questions', 'users')
)
class AnswerChoices(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid4,
unique=True,
)
choices_text = models.CharField(
max_length=160,
blank=True,
null=True,
default=None,
)
questions = models.ForeignKey(
Questions,
related_name='questions',
on_delete=models.CASCADE,
)
class Questions2Users(models.Model):
questions = models.ForeignKey(
Questions,
on_delete=models.CASCADE
)
users = models.ForeignKey(
Users,
null=True,
blank=True,
on_delete=models.CASCADE,
)
answerchoices = models.ForeignKey(
AnswerChoices,
on_delete=models.CASCADE
)
admin.py
from django.contrib import admin
from .models import (
User,
Questions,
Answers,
Questions2Users,
)
class Questions2UsersInline(admin.TabularInline):
model = Questions2Users
class UserAdminSite(admin.ModelAdmin):
class Meta:
model = User
inlines = [
Questions2UsersInline
]
admin.site.register(User, UserAdminSite)
I have identified it as a user model admin page with an inline for the M2M model. How would I display the questions queryset all at once with question_text and choice_text?
I have a model called Order with a datetime field called start. I can read and write from/to that field no problem.
However, I just created a ModelForm and specified start as one of the fields=() in Meta and I get:
Unknown field(s) (start) specified for Order
I've made sure it is not a typo by copying and pasting the field name. If I remove that field, it works.
Here is the exact ModelForm
class OrderForm(ModelForm):
class Meta:
model = Order
fields = ('details', 'start', 'quantity', 'total')
EDIT added more details:
I tried using exclude = () to exclude all fields except those I need, and start does not appear in the form even though I don't exclude it.
Here is the model:
class Order(MyModel):
user = models.ForeignKey(User, )
invoice = models.ForeignKey(Invoice, )
unit_price = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True, )
subtotal = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null =True, )
tax = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True, )
misc = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, )
total = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True, )
start = models.DateTimeField(auto_now_add=True, blank=True, null=True, )
end = models.DateTimeField(editable=True, blank=True, null=True, )
duration = models.PositiveSmallIntegerField(blank=True, null=True, )
quantity = models.PositiveSmallIntegerField(blank=True, null=True, )
notes = models.CharField(max_length=256, blank=True, null=True, )
details = models.CharField(max_length=64, blank=True, null=True, )
configured = models.BooleanField(default=False, )
Remove:
auto_now_add=True
Model field reference | Django documentation | Django :
As currently implemented, setting auto_now or auto_now_add to True
will cause the field to have editable=False and blank=True set.
I removed the auto_now_add=True and the problem is solved.
Thanks for everyone's help.
Maybe you have editable=False defined for the start field?
According to documentation:
If False, the field will not be displayed in the admin or any other ModelForm. Default is True.