Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
authtools: do not create one-to-one profile object on user creation.

Hello,

I am using the authtools plugin to manage the user and its profile in django, but when I create the user, it does not create its profile, I have to go to the admin part of the site and create it manually.

I separated the applications into account and profile.

This is the profiles model:

class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
primary_key=True)
slug = models.UUIDField(default=uuid.uuid4, blank=True, editable=False)
email_verified = models.BooleanField("Email verified", default=True)


This is the signal.py, that is inside of the profiles application:

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_profile_handler(sender, instance, created, **kwargs):
if not created:
return
profile = models.Profile(user=instance)
profile.save()
logger.info('New user profile for {} created'.format(instance))

This is the admin.py of the account app:

class UserProfileInline(admin.StackedInline):
model = Profile


class NewUserAdmin(NamedUserAdmin):
inlines = [UserProfileInline]
list_display = ('is_active', 'email', 'name', 'permalink',
'is_superuser', 'is_staff',)

# 'View on site' didn't work since the original User model needs to
# have get_absolute_url defined. So showing on the list display
# was a workaround.
def permalink(self, obj):
url = reverse("profiles:show",
kwargs={"slug": obj.profile.slug})
# Unicode hex b6 is the Pilcrow sign
return format_html('<a href="{}">{}</a>'.format(url, '\xb6'))


admin.site.unregister(User)
admin.site.register(User, NewUserAdmin)
admin.site.register(Profile)


When I signup a user, both the user and the profile objects are created, only they are not linked. Why is that?

Thank you




/r/django
https://redd.it/8q4ob3
Add multiple ordered similar arguments to manage command

I'm trying to build out a command that'll looker similar to this

`python manage.py gen-data primary=10-state with=30-users with=30-visits`

The goal here is to do test-data generation where the command doesn't become crazy for any of our front end devs. That command would do something like create 10 states, create 30 users who live in each state, and create 30 web visits for each of those users.

I know how to do the actual generation of the data, but I can't seem to find out how to handle having the same argument repeated like that.

Any help would be much appreciated.

Solved:
Turns out you can do a list object as a arg

parser.add_argument(
'--with', default=[], dest='withs', action='append',
help="Specify district ids by comma separated list.")

` python manage.py generate_data --model=user --with=Super --with=cali --with=frag --with=ilistice --with=xpial --with=idocious`

`['Super', 'cali', 'frag', 'ilistice', 'xpial', 'idocious']`


/r/django
https://redd.it/8q9scf
[D] Improving Language Understanding with Unsupervised Learning
https://blog.openai.com/language-unsupervised/

/r/MachineLearning
https://redd.it/8qbzom
[P] Simple Tensorflow implementation of StarGAN (CVPR 2018 Oral)

/r/MachineLearning
https://redd.it/8qh7e5
What's everyone working on this week?

Tell /r/python what you're working on this week! You can be bragging, grousing, sharing your passion, or explaining your pain. Talk about your current project or your pet project; whatever you want to share.


/r/Python
https://redd.it/8qj87y
What's the right way to handle these workflows in DRF?

I've never that been that into external APIs. We honestly haven't needed them that much, and when we have, API views written for each situation has both made sense and (obviously) done exactly what we need.

We're planning to open some of our system up to our clients (and their clients). It's essentially a booking system with, amongst other thins, Booking and User Models.

Basic viewsets and permissions are all done. 10 minute job. But there are two operations that I'm finding difficult to translate (in my head as much as anywhere else) into whatever passes for the "proper" way to do in REST.

The first is booking state change. Advancing the booking from EDITING to CONFIRMED or REJECTED or ON_SITE or LEFT_SITE or CANCELLED, etc. There are quite a few. In the "old" views we have logic to say what states some users can go between. There are also availability checks performed when going from EDITING to a confirmed state.

Do I just assume somebody is going to `partial_update` or 'update' the state and I have to validate this? Or should I be abstracting state change to '/bookings/<id>/{cancel,edit,reject,arrived,left}` etc?

The other problem is some views *occasionally* need more data. One example is a user list. Normally the list of users is enough but occasionally we might want to show (even sort) by when a User last had a booking.

In a Django view this is pretty trivial to annotate in. It's an expensive query, but it's trivial. And that's the problem. There are some Viewsets where I don't want all these expensive annotated fields littering the response where they aren't needed.

Do I just hack in a `?booking_data=1`, or *should* I add a separate API view for Users with booking data?

Again, I realise there are many simple things I could do to hack these in but this is intended for an external audience who have to read —and understand— documentation to use our product. I'm conscious that doing everything via hacky querystrings might not give the best impression.

/r/django
https://redd.it/8qkwqv
[D] Keys to compete against industry when in academia

It seems from published papers that state\-of\-the\-art results are always coming out from industry, and that makes sense because of the much stronger compute power typically available in companies vs. universities. Traditionally, it is somewhat expected that university researchers should focus more on ideas and concepts while industry researchers should consider large scale implementations. However, one observation at recent NIPS/ICML/ICLR/CVPR/etc.. papers reveals a common trend of showing off empirical results. This has haunted me several times and resulted in papers being rejected. It is just too hard to generate empirical results as awesome as those from industry. I hence focus on the more theoretical and algorithmic aspects, but it gets so frustrating when reviewers only critique the empirical results. My question is how to compete with industry in the era where empiricism is becoming so strong in machine learning?

/r/MachineLearning
https://redd.it/8qmhmo
Somebody please help me convert some url patterns to the newer "path" syntax

So I was following what I thought was a Django 2.0 tutorial until we got [to this part](http://muva.co.ke/blog/lesson-4-defining-url-patterns-template-shop-products-django-2-0-python-3-6/) where I just couldn't continue because I can't understand how to read those URL patterns. I'm new to Django and only wanna consider the new "path" syntax for urls. Anyway, would somebody be kind enough to convert the patterns in the linked tutorial to be in the new syntax? By the way, I didn't expect this, had been getting along with what I thought was a version 2.0 tutorial, but oh, well.

/r/django
https://redd.it/8qnoy1
r/RoguelikeDev is running a summer dev-along tutorial series in python

Interested in making a traditional turn-based roguelike? Want to do it alongside other devs and beginners with a tutorial? Over at r/roguelikedev we're starting a [weekly summer event](https://i.imgur.com/EYJFgdI.png) on June 19th where you can do just that :D

Check out "[RoguelikeDev Does The Complete Roguelike Tutorial](https://www.reddit.com/r/roguelikedev/comments/8ql895/roguelikedev_does_the_complete_roguelike_tutorial/)" for more info, including the planned schedule.

The main tutorial we'll be following is in python, although some participants may opt to use other languages.

/r/Python
https://redd.it/8qnlcm