I'm creating a simple project in Django to further my knowledge of it and Python, essentially just going to store bookmarks and tags, both will have views, and there will be 3 models:
Tag
Bookmark
Bookmark_Tag (relationships)
I read the "Projects vs Apps" panel in the django documentation, but I'm still unsure of how to lay it out, right now it's all within one Bookmark App, should there be a seperate app for Bookmarks and a seperate app for Tags, and if so, where does the model for the relationships live?
Thanks!
No, you don't need a separate app for each. They're closely related, and it sounds like together they define your app, not separately. If later, you added another functionality to your site that used the same database and settings but didn't share much else with your current app, that would be another app in the same project.
See Django project models.py versus app models.py and Django: "projects" vs "apps" on this site as well as Django tips: laying out an application for some more guidelines.
If Bookmarks and Tags have a many-to-many relationship, and you need to add extra fields to that relationship (other than just the ids of the related objects) you can use a ManyToManyField and set the through table:
class Bookmark(models.Model):
# whatever fields you need on Bookmark
tags = models.ManyToManyField('Tag', through = 'BookmarkTag')
class Tag(models.Model):
# whatever fields you need on Tag
pass
class BookmarkTag(models.Model):
bookmark = models.ForeignKey(Bookmark)
tag = models.ForeignKey(Tag)
# whatever additional fields you need on the relationship
See the Django docs on Extra fields on many-to-many relationships for more info.
Related
I'm using PostgreSQL for the DB.
I have two models:
Project - can contain N Assets.
Asset - has a Foreign Key field that points to a Project. An Asset is related to 1 Project.
I want the user to be able to set the order of the Assets of a Project in the Django Admin in a handy way like ordering a list in the edit Project screen.
The order of the Assets is important in my business logic, but I don't want the user to set the order for each individual Asset (just to improve UX).
Is there a way to show a field in the Project admin screen that lists all the Assets that are related to the project and allows the user to order them as he wishes?
I did not find any solution to this other than adding a new field in the Asset modal to specify the order and handling the logic by myself (which is expected), however, I don't want it to be a field that has to be changed manually in each individual Asset.
By assuming your model structure:
Add a related_name in your Asset model
models.py:
# imports ..
class Project(model.Models):
# other fields
class Asset(model.Models):
project_linked = models.ForeignKey(Project, related_name='related_assets', on_delete=models.CASCADE)
Then use that to list and oder projects in the admin project listing page:
admin.py:
from django.contrib import admin
# import the Project model
class Projectadmin(admin.ModelAdmin):
list_display = ["related_assets"] # add any other desired fields from the Project model
ordering = ["any_fields from model"] # replace the field with your preference
admin.site.register(Project, Projectadmin)
Upon specifying ordering list, the admin will have a toggle button on top of specified fields to re-order them.
I'm currently building a Django website where staff has control over users, and, within those powers, I'd like to add one where the staff members can add private comments on users so that they can be read by whoever has the power to do so.
So I started building the user models.py here what I did:
class user(models.Model):
name = models.CharField(max_length=30)
comments = models.TextField()
date = models.DateField(default=datetime.now())
def __str__(self):
return self.name
My question: how can I add a comment field every time a staff member wants to? Like, with the above code, I can only have one comment per user.
Everything is appreciated.
Thanks!
There are a few ways to do this.
1. ForeignKey
Make a separate model for the comments and have a ForeignKey to the User model. That way, multiple comments can be linked to the same user.
2. ArrayField
If you're using Postgres database, you can use the ArrayField.
Cons: Editing in the admin panel is not very user friendly.
3. JSONField
You can also keep multiple comments in a JSON array.
Cons: Editing in the admin panel is not user friendly.
P.S.: If you decide to use either ArrayField or JSONField, check out an app I've made called django-jsonform. This will make editing JSON and ArrayField nice and user-friendly.
I need some fields in my model for internal usages (i.e. status, last modified, etc.) which should only editable (and fillable) by python code.
How to hide it in Django Admin and disable direct editing from forms?
You have to setup Custom Django Admin to do the same.
https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.exclude
class ModelAdmin(admin.ModelAdmin):
exclude = ('field_1', 'field_2')
Hope this helps
I have client model which has a foreign key field to a country model.
So in Django admin, when I create a client and I select the country where this client belongs to. but the problem is the select list is too long(too many countries on this planet). Sometimes it takes just too long to get the one I need.
So I wonder if there is other widget in djano admin that provides a select-input-combo widget.
When I type in the input and it will filter out the right one for me to select.
Hope you can understand what I need here.
did you see raw_id_fields ?
you would do in admin.py something like:
class ClientAdmin(admin.ModelAdmin):
raw_id_fields = ("country",)
admin.site.register(Client, ClientAdmin)
then select widget will become something like:
Since Django 2.0 there is autocomplete_fields. From the documentation:
autocomplete_fields is a list of ForeignKey and/or ManyToManyField fields you would like to change to Select2 autocomplete inputs.
and
The Select2 input looks similar to the default input but comes with a search feature that loads the options asynchronously. This is faster and more user-friendly if the related model has many instances.
Note that you need to define search_fields in the related object's ModelAdmin since it is used by the widget.
Lets say for example I have a Model called "Client" and a model called "PhoneNumbers"
class PhoneNumbers(models.Model):
number = forms.IntegerField()
class Client(models.Model):
number = forms.ManyToManyField(PhoneNumbers)
Client has a ManyToMany relationship with PhoneNumbers. PhoneNumbers has almost 500,000 records in it so when it comes to editing a Client record from a model form with a MultiSelect widget that comes with a M2M filed, it takes forever to load. In fact, it never does. It just sits there trying to load all of those phone objects I am assuming.
My workaround was to so some tedious things with ajax and jquery to edit only the phone numbers in a Client record. Before wasting my time with all of that I wanted to see if there is somehow another way to go about it without having my page hang.
You need to create a custom widget for this field that lets you autocomplete for the correct record. If you don't want to roll your own: http://django-autocomplete-light.readthedocs.io/
I've used this for its generic relationship support, the M2M autocomplete looks pretty easy and intuitive as well. see video of use here: http://www.youtube.com/watch?v=fJIHiqWKUXI&feature=youtu.be
After reading your comment about needing it outside the admin, I took another look at the django-autocomplete-light library. It provides widgets you can use outside the admin.
from dal import autocomplete
from django import forms
class PersonForm(forms.ModelForm):
class Meta:
widgets = {
'myformfield': autocomplete.ModelSelect2(
# ...
),
}
Since Django 2.0, Django Admin ships with an autocomplete_fields attribute that generates autocomplete widgets for foreign keys and many-to-many fields.
class PhoneNumbersAdmin(admin.ModelAdmin):
search_fields = ['number']
class ClientAdmin(admin.ModelAdmin):
autocomplete_fields = ['number']
Note that this only works in the scope of Django admin of course. To get autocomplete fields outside the admin you would need an extra package such as django-autocomplete-light as already suggested in other answers.
Out of the box, the model admin has a raw_id_fields option that let your page load much quicker. However, the user interface of raw id fields isn't very intuitive, so you might have to roll your own solution.
We use this 3rd party widget for this:
https://github.com/crucialfelix/django-ajax-selects
Btw, your 'example' above is really bad DB design for a bunch of reasons. You should just have the phone number as a text field on the Client model and then you would have none of these issues. ;-)