I have two classes say A and b in django models
class Address(models.Model):
address_1 = models.CharField(max_length=128)
address_2 = models.CharField(max_length=128,blank=True)
class Facility(models.Model):
name = models.CharField(max_length=250, unique=True)
location = models.ForeignKey(Address)
I want to create django admin form so that I can edit both Facility name and address when I add Facility object in admin interface.
I dont want the reverse, means I dont want to edit Facility name when I add Address object.
Any help will be appreciated.
Do you know about inline admin?
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin
Related
I have two models:
Python 3.7
class ClassOne(models.Model):
name = models.CharField(max_length=10, default='')
class ClassTwo(models.Model):
name = models.CharField(max_length=20, default='')
class_ones = models.ManyToManyField(ClassOne)
Now I would like to show ClassOne in django-admin with all ClassTwo's listed. I already tried to create a Tabular (admin.TabularInline) or create a method in ClassOne as following:
def get_class_twos(self):
return self.classtwo_set.all()
and include that method in the fieldsets, but that did not work either. Neither did directly putting classtwo_set.all() or classtwo_set in the fieldset list.
You first should define TabularInline:
class ClassOneInLine(admin.TabularInline):
model = ClassTwo.classone.through
form = ClassOneForm
Then add it to admin class for ClassTwo:
class AuthorAdmin(admin.ModelAdmin):
inlines = (ClassOneInLine,)
And don't forget to register admin.
I want the user to be able to add values to an attribute 'county' in model CountyChoices. I then want those values to surface as choices in a form for address for the attribute 'county' in model Address.
I couldn't think of another way to explain this and so I had a hard time finding this in the documentation. What would this be called in the Django docs so that I can look this up?
You can try using Django ForeignKey
class Address(models.Model):
conutry = models.ForeignKey('Country')
class Country(models.Model):
name = models.CharField(max_length=50, unique=True)
My model is:
class CustomerAccount(models.Model):
name = models.CharField(max_length=50)
class MyUser(AbstractUser):
customer_account = models.ManyToManyField(CustomerAccount, related_name='users', blank=True)
default_customer_account = models.ForeignKey(CustomerAccount, related_name='users_using_default_account', null=True, blank=True)
I want to display in the admin interface of the CustomerAccount this sort of thing:
I don't need to add a MyUser in the CustomerAccount interface.
Most SO questions and docs are related to show an Inline class in the admin, but I don't need it.
How should I do?
The functinality shown above you get by making adding the desired field to the filter_horizontal list (docu).
Django has a unique_for_date property you can set when adding a SlugField to your model. This causes the slug to be unique only for the Date of the field you specify:
class Example(models.Model):
title = models.CharField()
slug = models.SlugField(unique_for_date='publish')
publish = models.DateTimeField()
What would be the best way to achieve the same kind of functionality for a non-DateTime field like a ForeignKey? Ideally, I want to do something like this:
class Example(models.Model):
title = models.CharField()
slug = models.SlugField(unique_for='category')
category = models.ForeignKey(Category)
This way I could create the following urls:
/example/category-one/slug
/example/category-two/slug
/example/category-two/slug <--Rejected as duplicate
My ideas so far:
Add a unique index for the slug and categoryid to the table. This requires code outside of Django. And would the built-in admin handle this correctly when the insert/update fails?
Override the save for the model and add my own validation, throwing an error if a duplicate exists. I know this will work but it doesn't seem very DRY.
Create a new slug field inheriting from the base and add the unique_for functionality there. This seems like the best way but I looked through the core's unique_for_date code and it didn't seem very intuitive to extend it.
Any ideas, suggestions or opinions on the best way to do this?
What about unique_together?
class Example(models.Model):
title = models.CharField()
slug = models.SlugField(db_index=False)
category = models.ForeignKey(Category)
class Meta:
unique_together = (('slug','category'),)
# or also working since Django 1.0:
# unique_together = ('slug','category',)
This creates an index, but it is not outside of Django ;) Or did I miss the point?
I have 2 models in django, and im also using ModelForm, my question is the second model have a froreignkey of the 1, and i want to have one page when generating the form. It's possible, how to link the two forms in one page.
Class Event(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField()
class Date(models.Model):
id = models.AutoField(primary_key=True)
start = models.DateTimeField()
end = models.DateTimeField()
event = models.ForeignKey("Event")
I also have
class EventForm(ModelForm)
Class Date(ModelForm)
What i want is to create the event in one page in my templates.
Thanks.
If you want to have this on the Django Admin, then you need to use inline models.
If you plan to create your own form (using ModelForms), then you need to use inline formets.