django-nested-inline missing fields when adding new inline row - django

In our project, i've been making use of the latest version of django-nested-inlines v0.3.7 with django 1.7
I have the following definition in my django admin to display the inlines
class SpecsGroupedContentInline(NestedStackedInline):
model = SpecificationGroupedContent
extra = 1
fk_name = "specification"
fields = ["field_unit"]
class SpecsContentInline(NestedStackedInline):
model = SpecificationListContent
extra = 1
fk_name = "specification"
fields = ["content"]
class SpecsInline(NestedStackedInline):
model = Specification
inlines = [SpecsContentInline, SpecsGroupedContentInline]
extra = 1
fk_name = "product"
class ProductAdmin(NestedModelAdmin):
inlines = [
SpecsInline
]
list_display = ('name', 'sku')
form = ProductAdminForm
When the page loads, the page looks like this
Initial product page load
It's a bit hard to see but the specs section has a title, an inline for Listed Contents and an inline for Grouped Contents. Afterwards, there's an empty Specs section with an empty title, empty Listed Contents, and empty Grouped Contents.
When I press the "Add another Specification" button at the bottom of the Specification inline to add a new row, the result of the new entry looks like this
New Specification entry image
As you can see, the Grouped Content field "Field Unit" is missing. I'm not sure if my setup is incorrect, but i found this stack overflow entry with a similar setup to mine
Django nested inlines not working?
It is for a different error though but we have a similar inline setup so i was thinking this setup is ok for django-nested-inlines.
Also, I'm sure if it's related but is django-nested-inlines affected if a nested inline model is a foreign key of another model? The SpecificationsGroupedContent model is a foreign key of another model unlike the SpecificationsListContent. I did a quick test where the SpecsInline inlines contained either SpecsGroupedContentInline only or SpecsContentInline only. It was working perfectly when using only SpecsContentInline but the error occurs when using SpecsGroupedContentInline.
For reference, here are the models I'm using with the related model for SpecsGroupedContent
class Product(AuthorStampedModel):
name = models.CharField(max_length=100)
sku = models.CharField(unique=True, max_length=100, verbose_name='SKU')
summary = models.TextField(blank=True, default="")
def __unicode__(self):
return self.return_product_details()
def return_product_details(self):
return '%s: %s' % (self.sku, self.name)
class Specification(models.Model):
product = models.ForeignKey('Product', related_name='specs')
title = models.CharField(max_length=100)
def __unicode__(self):
return self.return_spec_string()
def return_spec_string(self):
return '%s: %s' % (self.product.return_product_details(), self.title)
class SpecificationListContent(models.Model):
specification = models.ForeignKey('Specification', related_name='list_content')
content = models.CharField(max_length=255)
class Meta:
verbose_name_plural = 'Specification Listed Contents'
def __unicode__(self):
return self.specification.return_spec_string()
class SpecificationGroupedContent(models.Model):
specification = models.ForeignKey('Specification', related_name='group_content')
field_unit = models.CharField(max_length=255)
class Meta:
verbose_name_plural = 'Specification Grouped Contents'
def __unicode__(self):
return self.specification.return_spec_string()
class SpecificationGroupedContentValue(models.Model):
specification_grouped_content = models.ForeignKey(
'SpecificationGroupedContent',
related_name='group_content_value'
)
value = models.CharField(max_length=255)
class Meta:
verbose_name_plural = 'Specification Grouped Content Values'
def __unicode__(self):
return self.specification_grouped_content.specification.return_spec_string()
Thanks

Related

Django - Get objects from the table which do not have a Foreignkey in another table

I am Django rest framework to return the list of objects who do not have a foreign key in another table. what queryset should I write to get those objects.
models.py
class Event(models.Model):
id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=100,default='')
description = models.TextField(blank=True,default='', max_length=1000)
link = models.URLField(null=True)
image = models.ImageField(null=True, blank=True)
organizer = models.CharField(max_length=100, default='')
timings = models.DateTimeField(default=None)
cost = models.IntegerField(default=1,null=True,blank=True)
def __str__(self):
return self.title
class Featured(models.Model):
event = models.ForeignKey(Event, null=True ,on_delete=models.PROTECT, related_name="event")
def __str__(self):
return self.event.title
class Meta:
verbose_name_plural = 'Featured'
views.py
class Upcoming2EventsViewSet(mixins.RetrieveModelMixin,mixins.ListModelMixin,viewsets.GenericViewSet):
serializer_class = Upcoming2Events
def get_queryset(self):
featured_events = Featured.objects.all().values_list('id')
return Event.objects.filter(id__in=featured_events)
# return Event.objects.exclude(id__in=featured_events.event.id)
# # return Event.objects.exclude(id__in = [featured_events.id])
serializers.py
class Upcoming2Events(serializers.ModelSerializer):
id = serializers.CharField(source='event.id')
title = serializers.CharField(source='event.title')
timings = serializers.DateTimeField(source='event.timings')
organizer = serializers.CharField(source='event.organizer')
class Meta:
model = Featured
fields = ['id','title','organizer','timings']
Error
Got AttributeError when attempting to get a value for field `id` on serializer `Upcoming2Events`.
The serializer field might be named incorrectly and not match any attribute or key on the `Event` instance.
Original exception text was: 'RelatedManager' object has no attribute 'id'.
Can you tell me what queryset should I write to get the only objects which are not present in the table Featured?
Also, what should I do to get only the upcoming 2 events from the Event table which are not present in the Featured table?
Note I am not supposed to use any flag value, can you provide some other solutions?
Based on the Informations you wrote here, i would suggest using a flag to determine a featured event. A second Model is useful if you want to provide more Informations on this specific for a featured event
like this:
class Event(models.Model):
id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=100,default='')
description = models.TextField(blank=True,default='', max_length=1000)
link = models.URLField(null=True)
image = models.ImageField(null=True, blank=True)
organizer = models.CharField(max_length=100, default='')
timings = models.DateTimeField(default=None)
cost = models.IntegerField(default=1,null=True,blank=True)
featured = models.BooleanField(default=False)
so you can directly use querysets to get what you want:
Event.objects.exclude(featured=True)
Event.objects.exclude(featured=True).order_by('-timings')[:2]
I would use ModelViewsets directly, hence you will use your model here.
views and serializers would look like this:
views.py
class Upcoming2EventsViewSet(viewesets.ReadyOnlyModelViewSet):
serializer_class = EventSerializer
queryset = Event.objects.exclude(featured=True).order_by('-timings')[:2]
serializers.py
class EventSerializer(serializers.ModelSerilizer):
class Meta:
model = Event
fields = ['id', 'title', 'organizer', 'timings']
As improvement i would provide filters instead of setting up different ViewSets for just filtering querysets.

How to allow an inline model with Foreign Key to be used only once?

My Goal:
When I add Inlines in admin, to add that Model Object, only one Inline Item with a particular Foreign Key should be allowed.
My Models:
class RateCard(models.Model):
name = models.CharField(max_length=100)
year = models.IntegerField(choices=YEAR_CHOICES, default=timezone.now().year)
class BillingCategory(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "Billing Categories"
class BillingCatagoryRate(models.Model):
billing_category = models.ForeignKey(BillingCategory, on_delete=models.CASCADE)
billing_rate = models.PositiveIntegerField()
rate_card = models.ForeignKey(RateCard,
on_delete=models.CASCADE, blank=True, null=True)
Refer the admin screenshot.
I can select the Drop Down with same value more than once and save successfully. What I need is, RateCard should have only one BillingCatagory used once . NOT multiple (like shown in the picture)
My Ideas:
During Save, may be we can check if there are any duplicates in Billing Categories.
This is how I finally solved it . I added a check in admin.py for the ModelAdmin class.
We need to override the "save_formset" method to access the inline values . This is how my RateCardAdmin looks.
class RateCardAdmin(admin.ModelAdmin):
model = RateCard
list_display = ('name', 'year')
inlines = [BillingCatagoryRateInline]
def save_formset(self, request, form, formset, change):
ids = []
for data in formset.cleaned_data:
ids.append(data['billing_category'])
common_ids = [item for item , count in collections.Counter(ids).items() if count > 1]
if common_ids:
raise ValidationError('Billing Category Duplicate found')
formset.save()

In Django REST framework, how do you access a field in a table referenced by a foreign key

I have the following models in Django:
class campaign(models.Model):
start_date = models.DateField('Start Date')
end_date = models.DateField('End Date')
description = models.CharField(max_length=500)
name = models.CharField(max_length=200)
active_start_time = models.TimeField()
active_end_time = models.TimeField()
last_updated = models.DateTimeField('Date updated',auto_now=True)
active = models.BooleanField(default=True)
client_id = models.ForeignKey('client',on_delete=models.PROTECT)
def __unicode__(self):
return u'%d | %s | %s' % (self.id,self.name, self.description)
class campaign_product(models.Model):
product_id = models.ForeignKey('product',on_delete=models.PROTECT)
last_updated = models.DateTimeField('Date updated',auto_now=True)
campaign_id = models.ForeignKey('campaign',on_delete=models.PROTECT)
class product(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=500)
sku = models.CharField(max_length=200,blank=True,null=True)
retail_price = models.DecimalField(decimal_places=2,max_digits=11)
discount_price = ((1,'Yes'),(0,'No'))
discounted_price = models.DecimalField(decimal_places=2,max_digits=11,blank=True,null=True)
category_id = models.ForeignKey('category',on_delete=models.PROTECT)
last_updated = models.DateTimeField('Date updated',auto_now=True)
active = models.BooleanField(default=True)
def __unicode__(self):
return u'%d | %s' % (self.id, self.name)
I also have the following serializer:
class campaignProductSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = campaign_product
fields = ('product_id', 'campaign_id')
And the following view set behavior in the urls.py file:
class campaignProductViewSet(viewsets.ModelViewSet):
queryset = campaign_product.objects.filter(campaign_id__start_date__lte=datetime.now(),campaign_id__end_date__gte=datetime.now(),campaign_id__active__exact=True)
serializer_class = campaignProductSerializer
My problem is I need to include the name field from the products model in my query results when for instance a request is made on http://127.0.0.1:8000/campaign_product/1/. Currenly this request returns only the product_id and the campaign_id. I tried making the serializer as follows:
class campaignProductSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = campaign_product
fields = ('product_id', 'campaign_id', 'product.name')
But then the service returns the following error:
Field name `product.name` is not valid for model `campaign_product`.
I event tried using product__name with and without quotes. Without quotes it tells me that there is no such variable, and with quotes it gives the is not valid for model error similar to the above. Heeelp! Getting this extra field is proving to be a pain :-(
What you want will need to look something more like this:
class campaignProductSerializer(serializers.HyperlinkedModelSerializer):
product_name = serializers.CharField(source='product_id.name')
class Meta:
model = campaign_product
fields = ('product_id', 'campaign_id', 'product_name')
P.S. As an unrelated side note, it is generally a convention in Python code to name classes with CamelCase, such as Campaign, CampaignProduct, Product, and CampaignProductSerializer.
Edit: P.P.S. Originally, I had put written the product_name field with source='product.name'. This was actually due to me looking at the code too quickly and making assumptions based on Django conventions. Typically, with a Django ForeignKey, you would name the ForeignKey field after the model you are linking to, rather than explicitly naming it with _id. For example, the CampaignProduct model would typically be written with product = ForeignKey(...) and campaign = ForeignKey(...). In the background, Django will actually use product_id and campaign_id as the database field names. You also have access to those names on your model instances. But the product and campaign variables on your model instances actually return the objects which you are referring to. Hopefully that all makes sense.

Limit number of relationship in ManyToManyField in django

I am using ManyToMany relationship in my code in a scenario where i have showrooms and their categories. Now a showroom can fall into maximum three categories and i have to validate it while saving. Below is my code:
##models.py
class Showrooms(models.Model):
name = models.CharField(max_length = 100)
contact_person = models.CharField(max_length = 100)
offer = models.TextField()
categories = models.ManyToManyField(Categories, null=True, blank=True, related_name='categories')
class Meta:
db_table = 'showrooms'
verbose_name_plural = "showrooms"
class Categories(models.Model):
category = models.CharField(max_length = 100)
image = models.ImageField(upload_to = showroom_upload_path, null=True, blank=True)
slug = models.SlugField(blank=True)
class Meta:
db_table = 'showroom_categories'
verbose_name_plural = "categories"
def __str__(self):
return self.category
everything is working fine except i am not able to put validation on number of categories per showroom. And i am not using it in views but just wanna to do it in admin.
Please help
Thanks
Edits
Ok.. I have solved my issue. I created a form in forms.py
class ShowroomsForm(forms.ModelForm):
class Meta:
model = Showrooms
def clean(self):
categories = self.cleaned_data.get('categories')
if categories and categories.count() > 3:
raise ValidationError('Maximum three categories are allowed.')
return self.cleaned_data
and added it to admin.py like below:
class ShowroomsAdmin(admin.ModelAdmin):
form = ShowroomsForm
admin.site.register(Showrooms, ShowroomsAdmin)
You could define a clean() method on your model an raise a validation error whenever a showroom gets assigned to more than 3 categories.
https://docs.djangoproject.com/en/1.5/ref/models/instances/#django.db.models.Model.clean
I had the same problem and used #Rakesh Kumar's solution. But then I got an error
django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form JobsForm needs updating.
The problem was that Rashid's form didn't have any form fields.
So I modified his solution just a little.
forms.py - I added fields
class ShowroomsForm(forms.ModelForm):
class Meta:
model = Showrooms
fields = "__all__"
def clean(self):
categories = self.cleaned_data.get('categories')
if categories and categories.count() > 3:
raise ValidationError('Maximum three categories are allowed.')
return self.cleaned_data
admin.py - this remained the same:
class ShowroomsAdmin(admin.ModelAdmin):
form = ShowroomsForm
admin.site.register(Showrooms, ShowroomsAdmin)
After that, it worked perfectly!
All I needed was to create a model form:
class ShowroomsForm(forms.ModelForm):
class Meta:
model = Showrooms
def clean(self):
categories = self.cleaned_data.get('categories')
if categories and categories.count() > 3:
raise ValidationError('Maximum three categories are allowed.')
return self.cleaned_data
and added it to admin.py like below:
class ShowroomsAdmin(admin.ModelAdmin):
form = ShowroomsForm
admin.site.register(Showrooms, ShowroomsAdmin)

Django Admin - Multiple Inlines

Having a bit of trouble using multiple inlines within my admin console over 3 models which im playing around with.
Models:
class carManufacturer(models.Model):
name = models.CharField(max_length=200)
country = models.CharField(max_length=200)
description = models.CharField(max_length=1000)
def __unicode__(self):
return self.name
class vehicleModel(models.Model):
carManufacturer = models.ForeignKey(carManufacturer)
model = models.CharField(max_length=200)
def __unicode__(self):
return self.model
class vehicleCode(models.Model):
vehicleModel = models.ForeignKey(vehicleModel)
variantCode = models.CharField(max_length=200)
variantBadge = models.CharField(max_length=200)
manuStart = models.DateTimeField('Manufacture Start Date')
manuFin = models.DateTimeField('Manufacture End Date')
def __unicode__(self):
return self.variantCode
What I'm looking to do is when I add a car Manufacturer I can add car Models via my inline, and when I am editing models, I can edit vehicle codes/variants via another inline.
I have an admin.py file I am using:
from Cars.models import carManufacturer, vehicleModel, vehicleCode
from django.contrib import admin
class modelInline(admin.TabularInline):
model = vehicleModel
extra = 0
class codeInline(admin.TabularInline):
variantCode = vehicleCode
extra = 0
class CarAdmin(admin.ModelAdmin):
fields = ['name', 'description', 'country']
inlines = [modelInline]
class VehicleModelAdmin(admin.ModelAdmin):
fields = ['carManufacturer','model']
#inlines = [codeInline]
admin.site.register(carManufacturer, CarAdmin)
admin.site.register(vehicleModel, VehicleModelAdmin)
As soon as I uncomment my second inline which uses the same method as the first I get the following error:
'model' is a required attribute of 'VehicleModelAdmin.inlines[0]'.
I am struggling to understand what I am doing wrong, especially since I have got the first inline working, any input would be much appreciated
The codeInline doesn't have model field for any TabularInline you do need model field like one above. It should have something like following
class codeInline(admin.TabularInline):
model = vehicleCode
extra = 0