These are chat archives for django/django

16th
Oct 2018
damionsilver98
@damionsilver98
Oct 16 2018 07:35

I’m having a little troubles . I’m trying to show an object from my database to my html webpage

I have it set up to render(request, homepage/index.html, {“quote”: quote}) . I then use the for loop in my index template but it will not show up, but I can use {{quote}} and get the object in the database. Any help?

Tanveer Ahmed
@tanveerobjects_twitter
Oct 16 2018 13:26
Screenshot from 2018-10-16 18-14-04.png
DETAIL: Key (gcategory_id)=(1) is duplicated.
I am getting this error when I did makemigrations and migrate I am using Postgresql Please help me :(
damionsilver98
@damionsilver98
Oct 16 2018 15:27
@tanveerobjects_twitter what’s the code you have? I can try to look at it and see .
Corey Vincent
@CoreyVincent
Oct 16 2018 18:06

Can anyone help me with Django admin forms?
I'm trying to automatically set a field when it's saved, but it doesn't seem to work.
My understanding was to do the following:

# models.py
class BlogPost(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    posted_date = models.DateField(default=timezone.now)
    title = models.CharField(max_length=200)
    text = models.TextField(max_length=1000)
# in admin.py
class BlogPostAdmin(admin.ModelAdmin):
    model = BlogPost
    def formfield_for_choice_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'author':
            kwargs['queryset'] = User.objects.filter(username=request.user.username)
        return super(BlogPostAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

Preferably I would like the field to just be hidden when editing or creating a new BlogPost.

AJ
@ajostergaard
Oct 16 2018 19:38
@CoreyVincent I don't understand why you don't just set the field in your model's save function and include it in ModelAdmin.exclude. Which field are you trying to set?
AJ
@ajostergaard
Oct 16 2018 19:46
@damionsilver98 not sure I entirely follow - you can display quote but not in a loop? Is quote iterable?
@tanveerobjects_twitter the table you are trying to create a unique index on already has rows in it which are not unique. If the data is not important then empty the table...